Fresh install Python 3.9.0 or any other way to fix No module named '_ctypes' error

3.2k views Asked by At

Since installing to Python 3.9.0, I am unable to install any libraries to my virtualenv. When trying to install libraries via pip for example I get:

$ pip3 install -r requirements.txt
Collecting anaconda==0.0.1.1
  Using cached anaconda-0.0.1.1.tar.gz (726 bytes)
    ERROR: Command errored out with exit status 1:
     command: /home/jack/dev/37/env/bin/python3 -c 'import sys, setuptools, tokenize; sys.argv[0] = '"'"'/tmp/pip-install-01fhtisu/anaconda/setup.py'"'"'; __file__='"'"'/tmp/pip-install-01fhtisu/anaconda/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 /tmp/pip-pip-egg-info-b71dl332
         cwd: /tmp/pip-install-01fhtisu/anaconda/
    Complete output (11 lines):
    Traceback (most recent call last):
      File "<string>", line 1, in <module>
      File "/home/jack/dev/37/env/lib/python3.9/site-packages/setuptools/__init__.py", line 23, in <module>
        from setuptools.dist import Distribution
      File "/home/jack/dev/37/env/lib/python3.9/site-packages/setuptools/dist.py", line 34, in <module>
        from setuptools import windows_support
      File "/home/jack/dev/37/env/lib/python3.9/site-packages/setuptools/windows_support.py", line 2, in <module>
        import ctypes
      File "/usr/local/lib/python3.9/ctypes/__init__.py", line 8, in <module>
        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.

I have seen in a few places that this should be fixed by installing libffi-dev then reinstalling python. Here for example: Python3: ImportError: No module named '_ctypes' when using Value from module multiprocessing

What is the best way to go about doing this? I am on Ubuntu 20.04 and running python -V outside of a virtualenv reuturns Python 3.9.0.

To prevent this happening in the future is it generally recommended to only ever install newer versions on python (or any other language) in a venv and not on the system itself?

Many thanks.

0

There are 0 answers