Python3: ImportError: No module named '_ctypes' when using Value from module multiprocessing
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 toinstall
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:
- Download Python from https://www.python.org/ftp/python/3.7.0/Python-3.7.0.tar.xz
- Extract the content in new folder
- Open Terminal in the same directory
- 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
Related videos on Youtube
htc_m8
Updated on January 04, 2022Comments
-
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 over 9 yearsIt 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 over 9 yearsI installed from the source code using the sudo make install command. Yes importing Value from multiprocessing worked in 3.3.2
-
Eryk Sun over 9 years
_ctypes
wasn't built because thelibffi-dev
dependency wasn't available. Consider using the deadsnakes PPA instead. -
htc_m8 over 9 yearsInstalling libffi-dev and re-installing python3.4 fixed the problem for me
-
-
Veky over 7 yearsFantastic. Now, can you tell me how you got the list above? By trial and error? :-O
-
MikeiLL over 7 yearsHonestly, @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 over 6 yearsthis 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 over 5 yearsIt should be
python3-dev
instead ofpython-dev
.python-dev
seems forpython2
. -
Ryan over 5 yearsWhy 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 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 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
andmake
stuff requires those things to be installed. -
Oddstr13 about 5 yearsAdditional dependencies for optional libraries;
liblzma-dev
uuid-dev
libreadline-dev
(for Python 3.7 at on Debian 9 least) -
tushar_ecmc almost 5 yearsI have installed
libffi-dev
but I am still getting this error -
Mariusz over 4 yearsafter 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 over 4 yearsThe 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 over 4 yearsGreat 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 about 4 yearsWelcome to stack overflow. Can you provide more information? References etc and have a look at stackoverflow.com/help/how-to-answer
-
dragon788 about 4 yearsThanks 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 themake
stuff for sure. -
Ben-xue about 4 yearsIf you are using linux,execute
ldconfig
to load the newlibffi.so
. The main purpose ofapt-get install libffi-dev
is to install an file calledlibffi.so
which you can check it bydpkg -L libffi-dev
@tushar_ecmc -
Phil about 4 yearsThis worked for me on Ubuntu. Simple and effective.
-
Paweł Mucha about 4 yearsThanks, that was helpful. For full list of prerequisite for pyenv visit github.com/pyenv/pyenv/wiki/Common-build-problems#prerequisites
-
nedned about 4 yearsNote 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 about 4 yearsI 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 almost 4 yearsYou do not need to uninstall Python first. Just installing it over again will recompile Python binary and that's what's needed.
-
Pulkit Pahwa over 3 yearsI tried each one of the answers above but with no success. Only your answer worked. Thanks
-
zs2020 over 3 yearsYup, reinstalling python is necessary.
-
babis21 over 3 yearsThat 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 about 3 yearsInstalling libffi-dev and re-installing python3.8.7 did the trick for me! Thanks @steve-e
-
con about 3 yearsthis does not work anymore with CentOS.
No package libffi-devel is available
-
Arman Babaei about 3 yearsI needed to run
./confugure
with flag--with-system-ffi
to make it use the built libffi. -
Murtaza Haji about 3 yearsShameful copy paste from Github issue.
-
Murtaza Haji about 3 yearsFor me it fails with error
generate posix-vars failed
. Any idea? -
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 about 3 yearsThis link helped me solve the same issue. (running CentOs7) mirror.centos.org/centos/7/os/x86_64/Packages/…
-
slowkoni about 3 yearsMind 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 almost 3 yearsSolved it on CentOS as well! :+1:
-
Minek Po1 almost 3 yearsHow can I specify ffi path if its installed in a non default location?
-
Priyesh Patel almost 3 yearsThanks, this also worked in windows bash/ubuntu when installing python3.8
-
Saddle Point over 2 yearsSaved my day man!
-
Ouss over 2 yearsPlease quote or summerize the solution you are referring to here, in your answer.
-
Ouss over 2 yearsplease quote or summerize the answer you are referring to here so that your answer is more clear!
-
Flair over 2 yearsWhile 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 over 2 yearsThanks 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 about 2 yearsI have the LinuxBrew and have this issue too. Force to use Homebrew GCC work for me. Thanks.
-
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 about 2 yearsThank you, this worked for me on Debian (in WSL)
-
Matt Swezey about 2 yearsThis worked for me on Ubuntu 20.04, pyenv, python 3.8.13, & brew