Python3: ImportError: No module named '_ctypes' when using Value from module multiprocessing

298,440

Solution 1

Installing libffi-dev and re-installing python3.7 fixed the problem for me.

to cleanly build py 3.7 libffi-dev is required or else later stuff will fail

If using RHEL/Fedora:

yum install libffi-devel

or

sudo dnf install libffi-devel

If using Debian/Ubuntu:

sudo apt-get install libffi-dev

Solution 2

On a fresh Debian image, cloning https://github.com/python/cpython and running:

sudo apt-get update
sudo apt-get upgrade
sudo apt-get dist-upgrade
sudo apt-get install build-essential python-dev python-setuptools python-pip python-smbus
sudo apt-get install libncursesw5-dev libgdbm-dev libc6-dev
sudo apt-get install zlib1g-dev libsqlite3-dev tk-dev
sudo apt-get install libssl-dev openssl
sudo apt-get install libffi-dev

Now execute the configure file cloned above:

./configure
make # alternatively `make -j 4` will utilize 4 threads
sudo make altinstall

Got 3.7 installed and working for me.

SLIGHT UPDATE

Looks like I said I would update this answer with some more explanation and two years later I don't have much to add.

  • this SO post explains why certain libraries like python-dev might be necessary.
  • this SO post explains why one might use the altinstall as opposed to install argument in the make command.

Aside from that I guess the choice would be to either read through the cpython codebase looking for #include directives that need to be met, but what I usually do is keep trying to install the package and just keep reading through the output installing the required packages until it succeeds.

Reminds me of the story of the Engineer, the Manager and the Programmer whose car rolls down a hill.

Solution 3

If you use pyenv and get error "No module named '_ctypes'" (like i am) on Debian/Raspbian/Ubuntu you need to run this commands:

sudo apt-get install libffi-dev
pyenv uninstall 3.7.6
pyenv install 3.7.6

Put your version of python instead of 3.7.6

Solution 4

Detailed steps to install Python 3.7 in CentOS or any redhat linux machine:

  1. Download Python from https://www.python.org/ftp/python/3.7.0/Python-3.7.0.tar.xz
  2. Extract the content in new folder
  3. Open Terminal in the same directory
  4. Run below code step by step :
sudo yum -y install gcc gcc-c++ 
sudo yum -y install zlib zlib-devel
sudo yum -y install libffi-devel 
./configure
make
make install

Solution 5

Thought I'd add the Centos installs:

sudo yum -y install gcc gcc-c++ 
sudo yum -y install zlib zlib-devel
sudo yum -y install libffi-devel 

Check python version:

python3 -V

Create virtualenv:

virtualenv -p python3 venv

Share:
298,440

Related videos on Youtube

htc_m8
Author by

htc_m8

Updated on January 04, 2022

Comments

  • htc_m8
    htc_m8 over 2 years

    I am using Ubuntu and have installed Python 2.7.5 and 3.4.0. In Python 2.7.5 I am able to successfully assign a variable x = Value('i', 2), but not in 3.4.0. I am getting:

    Traceback (most recent call last):
       File "<stdin>", line 1, in <module>
       File "/usr/local/lib/python3.4/multiprocessing/context.py", line 132, in Value
          from .sharedctypes import Value
       File "/usr/local/lib/python3.4/multiprocessing/sharedctypes.py", line 10, in <
    module>
       import ctypes
       File "/usr/local/lib/python3.4/ctypes/__init__.py", line 7, in <module>
          from _ctypes import Union, Structure, Array
    ImportError: No module named '_ctypes'
    

    I just updated to 3.3.2 through installing the source of 3.4.0. It installed in /usr/local/lib/python3.4.

    Did I update to Python 3.4 correctly?

    One thing I noticed that Python 3.4 is installed in usr/local/lib, while Python 3.3.2 is still installed in usr/lib, so it was not overwritten.

    • mechanical_meat
      mechanical_meat over 9 years
      It appears that something got fouled up during the "installing the source of 3.4.0". What does that mean exactly? Was that command working in 3.3.2?
    • htc_m8
      htc_m8 over 9 years
      I installed from the source code using the sudo make install command. Yes importing Value from multiprocessing worked in 3.3.2
    • Eryk Sun
      Eryk Sun over 9 years
      _ctypes wasn't built because the libffi-dev dependency wasn't available. Consider using the deadsnakes PPA instead.
    • htc_m8
      htc_m8 over 9 years
      Installing libffi-dev and re-installing python3.4 fixed the problem for me
  • Veky
    Veky over 7 years
    Fantastic. Now, can you tell me how you got the list above? By trial and error? :-O
  • MikeiLL
    MikeiLL over 7 years
    Honestly, @Veky I dida a web search for the error, "ImportError: No module named '_ctypes'" and dug around, probably tried a few things before it worked. Will update answer to clarify with my scant understanding of APT, Python, make, etc.
  • abcd
    abcd over 6 years
    this was flagged as low quality, and i'm looking at it in the review queue. it really doesn't work as a stand-alone answer. i'm going to recommend deletion, but maybe you can improve it by expanding it?
  • Timo
    Timo over 5 years
    It should be python3-dev instead of python-dev. python-dev seems for python2.
  • Ryan
    Ryan over 5 years
    Why would you clone a source code repo, and then use sudo apt-get? That just doesn't make any sense. sudo apt-get handles the downloading of already compiled code, so why do you download the code and then not use it?
  • MikeiLL
    MikeiLL over 5 years
    @Ryan that's a great point. I don't remember how I came to this approach. Pretty sure I mostly found it somewhere. Looking back, I'm also not seeing any actual use of the cloned repository. Can you confirm that it will work without cloning the repo? If so, we should probably remove that line, or at least just change it to referencing the github repo in case someone wants to look at it.
  • Ryan
    Ryan about 5 years
    @MikeiLL I realized all the sudo statements are installing pre-requisites in order to go build the source code you downloaded. the ./configure and make stuff requires those things to be installed.
  • Oddstr13
    Oddstr13 about 5 years
    Additional dependencies for optional libraries; liblzma-dev uuid-dev libreadline-dev (for Python 3.7 at on Debian 9 least)
  • tushar_ecmc
    tushar_ecmc almost 5 years
    I have installed libffi-dev but I am still getting this error
  • Mariusz
    Mariusz over 4 years
    after installing libffi-devel it still didn't worked for me. It turned out i need to uninstall python first (as it was installed prior libffi-devel installation) and then reinstall it (python) again - from then on everything\ worked as expected.
  • user5915738
    user5915738 over 4 years
    The link is broken. I attempted to make the modification in the Makefile as you show here but it didn't work. _ctypes is still not building.
  • Aleksander Fular
    Aleksander Fular over 4 years
    Great answer actually. Everyone mentions that you need libffi-devel and that it would solve it, problem is that you also need to recompile python. I alsmot gave up on libffi.
  • mmoomocow
    mmoomocow about 4 years
    Welcome to stack overflow. Can you provide more information? References etc and have a look at stackoverflow.com/help/how-to-answer
  • dragon788
    dragon788 about 4 years
    Thanks for the answer, the highest voted had the full list I likely needed, but this reminded me to reinstall via pyenv in order to actually get things working. Using pyenv is way easier than grabbing the source code and doing all the make stuff for sure.
  • Ben-xue
    Ben-xue about 4 years
    If you are using linux,execute ldconfig to load the new libffi.so. The main purpose of apt-get install libffi-dev is to install an file called libffi.so which you can check it by dpkg -L libffi-dev @tushar_ecmc
  • Phil
    Phil about 4 years
    This worked for me on Ubuntu. Simple and effective.
  • Paweł Mucha
    Paweł Mucha about 4 years
    Thanks, that was helpful. For full list of prerequisite for pyenv visit github.com/pyenv/pyenv/wiki/Common-build-problems#prerequisi‌​tes
  • nedned
    nedned about 4 years
    Note that if you've compiled you're own Python distribution (eg through pyenv install), you'll need to re-compile the distribution after installing the package.
  • 5uperdan
    5uperdan about 4 years
    I was building python 3.8 for mint from source and kept having this problem. After installing this list though i rebuilt python and i no longer had the ctypes error
  • ruslaniv
    ruslaniv almost 4 years
    You do not need to uninstall Python first. Just installing it over again will recompile Python binary and that's what's needed.
  • Pulkit Pahwa
    Pulkit Pahwa over 3 years
    I tried each one of the answers above but with no success. Only your answer worked. Thanks
  • zs2020
    zs2020 over 3 years
    Yup, reinstalling python is necessary.
  • babis21
    babis21 over 3 years
    That worked for me on Python 3.9. I had 3.8 and it was working fine, then I installed 3.9 from source and pip install requirements was failing with the same error. After installing the suggested package and re built Python 3.9, it worked.
  • lgallard
    lgallard about 3 years
    Installing libffi-dev and re-installing python3.8.7 did the trick for me! Thanks @steve-e
  • con
    con about 3 years
    this does not work anymore with CentOS. No package libffi-devel is available
  • Arman Babaei
    Arman Babaei about 3 years
    I needed to run ./confugure with flag --with-system-ffi to make it use the built libffi.
  • Murtaza Haji
    Murtaza Haji about 3 years
    Shameful copy paste from Github issue.
  • Murtaza Haji
    Murtaza Haji about 3 years
    For me it fails with error generate posix-vars failed. Any idea?
  • RaveTheTadpole
    RaveTheTadpole about 3 years
    @con If your CentOS is old (which means CentOS 6 as of writing), I think the repos have disappeared? You can get packages from vault.centos.org/6.8/os/x86_64/Packages (adjust for your version and platform). I got libffi-devel from there.
  • BaldDude
    BaldDude about 3 years
    This link helped me solve the same issue. (running CentOs7) mirror.centos.org/centos/7/os/x86_64/Packages/…
  • slowkoni
    slowkoni about 3 years
    Mind sharing a link to that github issue @MurtazaHaji? The code above is directly from the source except the inserted line, so of course that is going to match github. The rest is my smack talk kind of writing which I can guarantee you is original and unlikely to how another would phrase the same thing, so if that content appears elsewhere, it was cut and paste from here. Curious as to the date of that github issue - and also whether they fixed it.
  • Mark
    Mark almost 3 years
    Solved it on CentOS as well! :+1:
  • Minek Po1
    Minek Po1 almost 3 years
    How can I specify ffi path if its installed in a non default location?
  • Priyesh Patel
    Priyesh Patel almost 3 years
    Thanks, this also worked in windows bash/ubuntu when installing python3.8
  • Saddle Point
    Saddle Point over 2 years
    Saved my day man!
  • Ouss
    Ouss over 2 years
    Please quote or summerize the solution you are referring to here, in your answer.
  • Ouss
    Ouss over 2 years
    please quote or summerize the answer you are referring to here so that your answer is more clear!
  • Flair
    Flair over 2 years
    While this link may answer the question, it is better to include the essential parts of the answer here and provide the link for reference. Link-only answers can become invalid if the linked page changes. - From Review
  • ajk
    ajk over 2 years
    Thanks for linking this here @n14s. Since you did me the favor of linking here, I've edited your answer to include a solution summary. Feel free to keep/modify/trash any of those changes of course. Cheers :).
  • AMing
    AMing about 2 years
    I have the LinuxBrew and have this issue too. Force to use Homebrew GCC work for me. Thanks.
  • AMing
    AMing about 2 years
    @n14s solution by forcing pyenv to use HomeBrew managed GCC work for me. Maybe a solution for those who want to keep Homebrew and pyenv in the same time.
  • Justin Palmer
    Justin Palmer about 2 years
    Thank you, this worked for me on Debian (in WSL)
  • Matt Swezey
    Matt Swezey about 2 years
    This worked for me on Ubuntu 20.04, pyenv, python 3.8.13, & brew