Quepinch

How to fix error: ModuleNotFoundError: No module named '_ctypes'

Sep 4, 2023

Question: I am getting the following error while I am trying to pipenv sync inside a project directory on ubuntu 22.04:

ModuleNotFoundError: No module named '_ctypes'

Answer: The error `ModuleNotFoundError: No module named '_ctypes' is not related to pipenv, but rather to the Python installation itself. This error usually occurs when the `_ctypes` module is not built during the Python installation, which can happen if the `libffi-dev` package is not installed on your system. You can try installing the `libffi-dev` package by running `sudo apt-get install libffi-dev` and then re-installing Python. If you are using a pre-installed version of Python that came with Ubuntu, you may need to build Python from source after installing the libffi-dev package.



Question: Now I installed libffi-dev using command "sudo apt install libffi-dev" but the problem still exists

Answer: After installing the `libffi-dev` package, you may need to rebuild Python from source to ensure that the `_ctypes` module is built correctly. If you are using a pre-installed version of Python that came with Ubuntu, you may need to build Python from source after installing the `libffi-dev` package.

If you are using `pyenv` to manage your Python versions, you can try uninstalling the Python version that you are using with pyenv uninstall <version>.

E.g.: pyenv uninstall 3.10.4



Author
Ankit Shah