293

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.

4
  • 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? Nov 19, 2014 at 17:17
  • I installed from the source code using the sudo make install command. Yes importing Value from multiprocessing worked in 3.3.2
    – htc_m8
    Nov 19, 2014 at 18:27
  • 17
    _ctypes wasn't built because the libffi-dev dependency wasn't available. Consider using the deadsnakes PPA instead.
    – Eryk Sun
    Nov 19, 2014 at 19:45
  • 19
    Installing libffi-dev and re-installing python3.4 fixed the problem for me
    – htc_m8
    Nov 19, 2014 at 21:21

22 Answers 22

445

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:

sudo yum install libffi-devel

or

sudo dnf install libffi-devel

If using Debian/Ubuntu:

sudo apt-get install libffi-dev
13
  • 97
    I have installed libffi-dev but I am still getting this error Jun 18, 2019 at 12:00
  • 9
    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
    – Ben-xue
    Mar 28, 2020 at 13:21
  • 96
    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.
    – nedned
    Apr 24, 2020 at 8:12
  • 6
    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.
    – babis21
    Dec 10, 2020 at 15:39
  • 1
    Installing libffi-dev and re-installing python3.8.7 did the trick for me! Thanks @steve-e
    – lgallard
    Feb 15, 2021 at 14:23
164

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.

9
  • 4
    Fantastic. Now, can you tell me how you got the list above? By trial and error? :-O
    – Veky
    Jan 20, 2017 at 7:11
  • 4
    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.
    – MikeiLL
    Jan 20, 2017 at 13:42
  • 1
    It should be python3-dev instead of python-dev. python-dev seems for python2.
    – Timo
    Sep 8, 2018 at 18:33
  • 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?
    – Ryan
    Feb 2, 2019 at 23:24
  • 1
    @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.
    – Ryan
    Feb 10, 2019 at 9:58
156

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

8
  • 1
    Welcome to stack overflow. Can you provide more information? References etc and have a look at stackoverflow.com/help/how-to-answer
    – mmoomocow
    Feb 24, 2020 at 11:25
  • 4
    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.
    – dragon788
    Feb 25, 2020 at 2:33
  • 2
    This worked for me on Ubuntu. Simple and effective.
    – Phil
    Apr 15, 2020 at 19:49
  • 4
    You do not need to uninstall Python first. Just installing it over again will recompile Python binary and that's what's needed.
    – ruslaniv
    Jul 27, 2020 at 15:58
  • 4
    Yup, reinstalling python is necessary.
    – zs2020
    Nov 23, 2020 at 16:02
31

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
3
  • 2
    this does not work anymore with CentOS. No package libffi-devel is available
    – con
    Feb 25, 2021 at 23:01
  • @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. Mar 7, 2021 at 3:32
  • This link helped me solve the same issue. (running CentOs7) mirror.centos.org/centos/7/os/x86_64/Packages/…
    – BaldDude
    Mar 12, 2021 at 21:25
21

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

1
  • 2
    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?
    – abcd
    Oct 5, 2017 at 17:40
19

On my Ubuntu 18.04 machine, I had the common problem of python not finding _ctypes with the pyenv installed python.

In my case libffi-dev was already installed. Installing cpython from source, as suggested by @MikeiLL, didn't help either.

Turned out to be an homebrew issue.

ajkerrigans suggested solution on pyenvs github issues solved this problem for me.

Solution summary: Tell pyenv to build Python using the Homebrew-managed GCC, with a command like:

CC="$(brew --prefix gcc)/bin/gcc-11" \
pyenv install --verbose 3.10.0

This assumes that any build dependencies have also been installed via Homebrew as specified in the pyenv wiki. As of this writing, that looks like this for Homebrew on Linux:

brew install bzip2 libffi libxml2 libxmlsec1 openssl readline sqlite xz zlib
7
  • Please quote or summerize the solution you are referring to here, in your answer.
    – Ouss
    Dec 30, 2021 at 12:18
  • please quote or summerize the answer you are referring to here so that your answer is more clear!
    – Ouss
    Dec 30, 2021 at 12:19
  • 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
    – Flair
    Jan 2, 2022 at 18:21
  • 1
    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 :).
    – ajk
    Jan 3, 2022 at 14:18
  • I have the LinuxBrew and have this issue too. Force to use Homebrew GCC work for me. Thanks.
    – AMing
    Feb 24, 2022 at 2:40
17

This solved the same error for me on Debian:

sudo apt-get install libffi-dev

and compile again

Reference: issue31652

15

None of the solution worked. You have to recompile your python again; once all the required packages were completely installed.

Follow this:

  1. Install required packages
  2. Run ./configure --enable-optimizations

https://gist.github.com/jerblack/798718c1910ccdd4ede92481229043be

3
13

I run into this error when I tried to install Python 3.7.3 in Ubuntu 18.04 with next command: $ pyenv install 3.7.3. Installation succeeded after running $ sudo apt-get update && sudo apt-get install libffi-dev (as suggested here). The issue was solved there.

10

Based on this answer, just copy-paste into the terminal.

First run: sudo apt-get -y update

then:

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

PS: You can just copy-paste the whole chunk into the terminal in one go.

5

In my case what was causing all sorts of Python installation issues including the one having to do with _ctypes and libffi was Homebrew on Linux / Linuxbrew. pyenv was happy again once brew was no longer in the $PATH.

1
  • @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.
    – AMing
    Feb 24, 2022 at 2:42
4

Refer to this thread or this thread, for customized installation of libffi, it is difficult for Python3.7 to find the library location of libffi. An alternative method is to set the CONFIGURE_LDFLAGS variable in the Makefile, for example CONFIGURE_LDFLAGS="-L/path/to/libffi-3.2.1/lib64".

1
  • 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. Jan 3, 2020 at 5:24
3

My solution: Installing libffi-dev with apt-get didn't help. But this helped: Installing libffi from source and then installing Python 3.8 from source.

My configuration: Ubuntu 16.04 LTS Python 3.8.2

Step by step:

I got the error message "ModuleNotFoundError: No module named '_ctypes'" when starting the debugger from Visual Studio Code, and when running python3 -c "import sklearn; sklearn.show_versions()".

  • download libffi v3.3 from https://github.com/libffi/libffi/releases
  • install libtool: sudo apt-get install libtool The file README.md from libffi mentions that autoconf and automake are also necessary. They were already installed on my system.
  • configure libffi without docs:

./configure --disable-docs

make check

sudo make install

After that my python installation could find _ctypes.

1
  • For me it fails with error generate posix-vars failed. Any idea? Mar 1, 2021 at 23:03
3

CentOS without root

  1. Install libffi-3.2 (Do NOT use libffi-3.3)

    wget ftp://sourceware.org/pub/libffi/libffi-3.2.tar.gz
    tar -xzf libffi-3.2.tar.gz
    cd libffi-3.2/
    ./configure --prefix=$YOUR_LIBFFI_DIR
    make && make install

  2. Install Python3

    ./configure --prefix=$YOUR_PATH/python/3.7.10 LDFLAGS=-L${YOUR_LIBFFI_DIR}/lib64 PKG_CONFIG_PATH=${YOUR_LIBFFI_DIR}/lib/pkgconfig --enable-shared

    make && make install

Thanks for JohnWSteill

1

I was having the same problem. None of the above solutions worked for me. The key challenge was that I didn't have the root access. So, I first download the source of libffi. Then I compiled it with usual commands:

./configure --prefix=desired_installation_path_to_libffi
make 

Then I recompiled python using

./configure --prefix=/home/user123/Softwares/Python/installation3/  LDFLAGS='-L/home/user123/Softwares/library/libffi/installation/lib64'
make 
make install

In my case, 'home/user123/Softwares/library/libffi/installation/lib64' is path to LIBFFI installation directory where libffi.so is located. And, /home/user123/Softwares/Python/installation3/ is path to Python installation directory. Modify them as per your case.

0

If you don't mind using Miniconda, the necessary external libraries and _ctypes are installed by default. It does take more space and may require using a moderately older version of Python (e.g. 3.7.6 instead of 3.8.2 as of this writing).

0

How to install Python from source without libffi in /usr/local?

  1. Download libffi from github and install to /path/to/local
  2. Download python source code and compile with the following configuration:
export PKG_CONFIG_PATH=/path/to/local/lib/pkgconfig
./configure --prefix=/path/to/python \
    LDFLAGS='-L/path/to/local/lib -Wl,-R/path/to/local/lib' \
    --enable-optimizations
make
make install
0

I am using MAC M1 and I had this error:

... __boot__.py", line 30, in <module> import ctypes
and something was said about the file libffi.8.dylib

I downloaded this thing on Anaconda and now everything works:

https://anaconda.org/wakari/libffi

I inform you, since much of the above is either not for MAC or outdated, my Python is on Anaconda version 3.10.4

Application file created with py2app works now!!

0

If your issue is with the VSCODE DEBUGGER, check your currently selected python interpreter. I had both python3.10.9 and python3.10.6 installed; however, the former was probably missing some dependencies so I switched to the latter(my OS default interpreter) which solved the issue.

To change your python interpreter in VSCODE:

  1. Hold ctrl+shift+P
  2. Search Python:Select Interpreter and try your OS default version(The version you get when you run python3 --version
  3. If the issue is still not resolved, run sudo apt-get install libffi-dev.
0

Similar case:

  • Environment: Windows 11, Python 3.11

  • Issue: use IDA to open binary file, error:

    E:\DevTools\iOSReverse\IDA\IDA7.5\IDAPro\loaders\uimage.py: No module named '_ctypes'
    Traceback (most recent call last):
    File "E:\DevTools\iOSReverse\IDA\IDA7.5\IDAPro\python\3\ida_idaapi.py", line 616, in IDAPython_ExecScript
    exec(code, g)
    File "E:/DevTools/iOSReverse/IDA/IDA7.5/IDAPro/loaders/uimage.py", line 79, in <module>
    import ctypes
    File "E:\DevInstall\Python311\Lib\ctypes\__init__.py", line 8, in <module>
    from _ctypes import Union, Structure, Array
    ModuleNotFoundError: No module named '_ctypes'
    
  • Solution: Add system environment variable:

    • PYTHONPATH = E:\DevInstall\Python311\Lib;E:\DevInstall\Python311\DLLs;E:\DevInstall\Python311\tcl;
    • PYTHONHOME=E:\DevInstall\Python311
  • Refer: ImportError : No module named '_ctypes'

    • Ctypes is included in the standard library

    • _ctypes.pyd is in PYTHONHOME/DLLs. You should include the directory in your PYTHONPATH environment variable

-1

You have to load the missing php3 (Python3) modules from the package manager. If you have Ubuntu I recommend the Synaptic Package Manager:

sudo apt-get install synaptic

There you can simply search for the missing modules. search for ctypes and install all the packages. Then go to your Python dir and do

./configure
make install.

This should solve your problem.

-3

If you are doing something nobody here will listen you about because "you're doing it the wrong way", but you have to do it "the wrong way" for reasons too asinine to explain and also beyond your ability to control, you can try this:

Get libffi and install it into your user install area the usual way.

git clone https://github.com/libffi/libffi.git
cd libffi
./configure --prefix=path/to/your/install/root
make
make install

Then go back to your Python 3 source and find this part of the code in setup.py at the top level of the python source directory

        ffi_inc = [sysconfig.get_config_var("LIBFFI_INCLUDEDIR")]
        if not ffi_inc or ffi_inc[0] == '':
            ffi_inc = find_file('ffi.h', [], inc_dirs)
        if ffi_inc is not None:
            ffi_h = ffi_inc[0] + '/ffi.h'
            if not os.path.exists(ffi_h):
                ffi_inc = None
                print('Header file {} does not exist'.format(ffi_h))
        ffi_lib = None
        if ffi_inc is not None:
            for lib_name in ('ffi', 'ffi_pic'):
                if (self.compiler.find_library_file(lib_dirs, lib_name)):
                    ffi_lib = lib_name
                    break

        ffi_lib="ffi"  # --- AND INSERT THIS LINE HERE THAT DOES NOT APPEAR ---
        if ffi_inc and ffi_lib:
            ext.include_dirs.extend(ffi_inc)
            ext.libraries.append(ffi_lib)
            self.use_system_libffi = True

and add the line I have marked above with the comment. Why it is necessary, and why there is no way to get configure to respect '--without-system-ffi` on Linux platforms, perhaps I will find out why that is "unsupported" in the next couple of hours, but everything has worked ever since. Otherwise, best of luck... YMMV.

WHAT IT DOES: just overrides the logic there and causes the compiler linking command to add "-lffi" which is all that it really needs. If you have the library user-installed, it is probably detecting the headers fine as long as your PKG_CONFIG_PATH includes path/to/your/install/root/lib/pkgconfig.

2
  • 1
    Shameful copy paste from Github issue. Mar 1, 2021 at 22:33
  • 1
    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.
    – slowkoni
    Mar 14, 2021 at 21:38

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.