'ModuleNotFoundError: No module named '_ctypes' Mac M1

While installing some libraries you may find the issue ModuleNotFoundError: No module named '_ctypes'



Solution 1:[1]

Make sure you are running python 3.8.10 +

Solution 2:[2]

Short version:

Try installing python 3.7.12 with pyenv: pyenv install 3.7.12, and if that does not work, try python 3.7.12 (pyenv install 3.7.10).

The pyenv release 2.2.3 addresses the compilation problems for 3.6.15/3.7.12 on M1 macs.

Long version:

The underlying cause for the _ctypes error seems to be that libffi cannot be found during the compilation, and is therefore (silently) skipped during the Python installation.

There is a comprehensive overview for installing different versions using pyenv at this page, some specific versions require homebrew patches. Here is an overview of those patches. However, I would try installing them without the patches first, as the pyenv team has fixed various compilation problems since that post was written.

The general syntax for installing with a patch is: pyenv install --patch X.X.X <<(curl -sSL link_to_patch) where X.X.X is the version you want to install.

Another solution is to use an x86 version of homebrew.

Officially, Python 3.7 and lower are not supported on Apple Silicon.

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 user3447228
Solution 2