pipはPythonに同梱されているパッケージマネージャであり、uWSGIはPythonで製作されたウェブアプリを実行するためのソフトウェアです。本記事ではpipでuWSGIをインストールする際、ModuleNotFoundError: No module named ‘_ctypes’でインストールが失敗する場合の対処法を紹介します。
libffiをインストールする
PythonのctypesはCで書かれたライブラリをPythonから利用するためのモジュールであり、libffiに依存しています。ソースからPythonをビルドする際、libfflが見つからない場合はctypesのビルドはスキップされます。ctypesがビルドされていないPythonからctypesを利用しようとすると、以下のように当該エラーが発生します。
$ pip3 install uwsgi Collecting uwsgi Downloading https://files.pythonhosted.org/packages/e7/1e/3dcca007f974fe4eb369bf1b8629d5e342bb3055e2001b2e5340aaefae7a/uwsgi-2.0.18.tar.gz (801kB) |████████████████████████████████| 808kB 6.0MB/s ERROR: Command errored out with exit status 1: command: /usr/local/bin/python3.8 -c 'import sys, setuptools, tokenize; sys.argv[0] = '"'"'/tmp/pip-install-_9_ib6w3/uwsgi/setup.py'"'"'; file='"'"'/tmp/pip-install-_9_ib6w3/uwsgi/setup.py'"'"';f=getattr(tokenize, '"'"'open'"'"', open)(file);code=f.read().replace('"'"'\r\n'"'"', '"'"'\n'"'"');f.close();exec(compile(code, file, '"'"'exec'"'"'))' egg_info --egg-base pip-egg-info cwd: /tmp/pip-install-_9_ib6w3/uwsgi/ Complete output (11 lines): Traceback (most recent call last): File "", line 1, in File "/usr/local/lib/python3.8/site-packages/setuptools/init.py", line 20, in from setuptools.dist import Distribution, Feature File "/usr/local/lib/python3.8/site-packages/setuptools/dist.py", line 35, in from setuptools import windows_support File "/usr/local/lib/python3.8/site-packages/setuptools/windows_support.py", line 2, in import ctypes File "/usr/local/lib/python3.8/ctypes/init.py", line 7, in from _ctypes import Union, Structure, Array ModuleNotFoundError: No module named '_ctypes' ---------------------------------------- ERROR: Command errored out with exit status 1: python setup.py egg_info Check the logs for full command output.
当該エラーを解消するには、libffiをインストールした上でPythonを再ビルド・再インストールする必要があります。Ubuntuの場合、libffiは以下のコマンドでインストールできます。
$ sudo apt-get install libffi-dev $ #または $ sudo apt install libffi-dev
libffiのインストール完了後、Pythonを再度インストールします。以下は基本的なPythonのダウンロード・インストール手順です。
$ wget https://www.python.org/ftp/python/3.8.0/Python-3.8.0.tgz $ tar -xzf Python-3.8.0.tgz $ cd Python-3.8.0/ $ ./configure $ make $ sudo make install
ctypesがビルドされたかどうかは、make時のログから確認することができます。以下はlibffiインストール前の状態でmakeしたときのログの抜粋です。
Python build finished successfully! The necessary bits to build these optional modules were not found: _bz2 _curses _curses_panel _dbm _gdbm _lzma _sqlite3 _tkinter _uuid readline To find the necessary bits, look in setup.py in detect_modules() for the module's name. The following modules found by detect_modules() in setup.py, have been built by the Makefile instead, as configured by the Setup files: _abc atexit pwd time Failed to build these modules: _ctypes
ログの最下部からctypesのビルドが失敗した旨を確認できます。ctypesのビルドが成功した場合はこのログが出力されません。ctypesのビルドの成功を確認したら、uWSGIのインストールを再度試します。
$ pip3 install uwsgi Collecting uwsgi Downloading https://files.pythonhosted.org/packages/e7/1e/3dcca007f974fe4eb369bf1b8629d5e342bb3055e2001b2e5340aaefae7a/uwsgi-2.0.18.tar.gz (801kB) |████████████████████████████████| 808kB 5.7MB/s Installing collected packages: uwsgi Running setup.py install for uwsgi … done Successfully installed uwsgi-2.0.18 $ pip3 freeze uWSGI==2.0.18
当該エラーが解消され、uWSGIが正常にインストールされます。
「pipでのuWSGIのインストールが”ModuleNotFoundError: No module named ‘_ctypes'”で失敗する」への1件のフィードバック
コメントは受け付けていません。