No module named '_ctypes'

Question:

I’m trying to install pyautogui, but pip keeps throwing errors. How to fix it? I’ve tried installing libffi library. Here is some code:

python3 -m pip install pyautogui
Defaulting to user installation because normal site-packages is not writeable
Collecting pyautogui
  Using cached PyAutoGUI-0.9.50.tar.gz (57 kB)
    ERROR: Command errored out with exit status 1:
     command: /usr/local/bin/python3 -c 'import sys, setuptools, tokenize; sys.argv[0] = '"'"'/tmp/pip-install-sxm4ewnq/pyautogui/setup.py'"'"'; __file__='"'"'/tmp/pip-install-sxm4ewnq/pyautogui/setup.py'"'"';f=getattr(tokenize, '"'"'open'"'"', open)(__file__);code=f.read().replace('"'"'rn'"'"', '"'"'n'"'"');f.close();exec(compile(code, __file__, '"'"'exec'"'"'))' egg_info --egg-base /tmp/pip-pip-egg-info-85ugzov6
         cwd: /tmp/pip-install-sxm4ewnq/pyautogui/
    Complete output (11 lines):
    Traceback (most recent call last):
      File "<string>", line 1, in <module>
      File "/usr/local/lib/python3.8/site-packages/setuptools/__init__.py", line 19, in <module>
        from setuptools.dist import Distribution
      File "/usr/local/lib/python3.8/site-packages/setuptools/dist.py", line 34, in <module>
        from setuptools import windows_support
      File "/usr/local/lib/python3.8/site-packages/setuptools/windows_support.py", line 2, in <module>
        import ctypes
      File "/usr/local/lib/python3.8/ctypes/__init__.py", line 7, 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.

That’s from python REPL

>>> sys.path
['', '/home/walenty/apps/Python-3.8.5/Modules/_ctypes', '/usr/local/lib/python38.zip', '/usr/local/lib/python3.8', '/usr/local/lib/python3.8/lib-dynload', '/home/walenty/.local/lib/python3.8/site-packages', '/usr/local/lib/python3.8/site-packages']
>>> import _ctypes
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
ModuleNotFoundError: No module named '_ctypes'

Asked By: Likepineapple

||

Answers:

okay, I’ve got it. This is the answer Python3: ImportError: No module named '_ctypes' when using Value from module multiprocessing

I cloned python3.10 from git and installed it from scratch.

Answered By: Likepineapple

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…

Answered By: Jonathan

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
Answered By: soniaseguz
Categories: questions Tags:
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.