Short version:
Try installing python 3.7.13 with pyenv:
pyenv install 3.7.13, and if that does not work, try python 3.7.12 (pyenv install 3.7.12).
The pyenv release 2.2.3 addresses the compilation problems for 3.6.15/3.7.12 on M1 macs, specifically for ctypes.
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.
Answer from rvd on Stack OverflowShort version:
Try installing python 3.7.13 with pyenv:
pyenv install 3.7.13, and if that does not work, try python 3.7.12 (pyenv install 3.7.12).
The pyenv release 2.2.3 addresses the compilation problems for 3.6.15/3.7.12 on M1 macs, specifically for ctypes.
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.
Make sure you are running python 3.8.10 +
What is Python ModuleNotFoundError and what causes it?
How do I fix 'ModuleNotFoundError: No module named X'?
Is ModuleNotFoundError the same as ImportError?
Required
Install foreign function interface headers
sudo apt install libffi-dev
Reinstall Python
Substitute desired python version
Ubuntu
sudo add-apt-repository ppa:deadsnakes/ppa -y && sudo apt install --reinstall python3.9-distutils
MacOS
Use brew install python3.9 or port install python3.9 (I recommend port)
Windows
Use Microsoft Store
Specify project python version
Poetry
poetry env use 3.9
Virtual envs
virtualenv -p python3.9 myproject
etc...
As other answers say, you need to install libffi-dev. If you're using pyenv/virtualenv, also reinstall the base python version:
Install libffi
sudo apt-get install libffi-dev
Load the new libffi.so, as suggested here
sudo ldconfig
Reinstall the python version available to pyenv
pyenv install 3.9.12
Finally, create the fresh virtualenv
pyenv virtualenv 3.9.12 new_environment