8

I'm trying to build Python-3.7.3 from source with ensurepip but i'm getting this error:

ModuleNotFoundError: No module named '_ctypes'

All of the answers online say that libffi-dev is needed but I have it installed and it still giving me this error.

root@4b6d672f1334:/Python-3.7.3# find / -name libffi.*
/usr/lib/pkgconfig/libffi.pc
/usr/lib/libffi.a
/usr/lib/libffi.so
/usr/lib/libffi.so.5.0.10
/usr/lib/libffi.so.5
/usr/share/info/libffi.info.gz

The build is in a container image from ubuntu:10.04. It is that old on purpose because I'm using PyInstaller to compile the application and it needs to run on machines with an old glibc (2.11) and this image is the only one that I could find that have this old version.

I have done the same for Python-2.7.16 and it worked without any issues.

Update Python-3.6.8 is working without any issues as well

hmn Falahi
  • 730
  • 5
  • 22
Amir Rossert
  • 1,003
  • 2
  • 13
  • 33
  • 3
    I just tried to build 3.7.3 from source on Lubuntu 18.04; it complained of missing `_ctypes`. Solution for me was to `sudo apt install libffi-dev` and re-build. So issue seems to be related to old Ubuntu version. – rdtsc Apr 29 '19 at 01:38
  • I tried it as well and it is working on newer Ubuntu versions, the issue is that I need an old glibc version (2.11) to support older distributions. – Amir Rossert Apr 29 '19 at 11:08

3 Answers3

6

I was able to find a solution here

The issue is probably with an old version of libffi-dev, the solution is to build and install libffi from source and then build Python3.7.3

Build libffi:

wget ftp://sourceware.org/pub/libffi/libffi-3.2.1.tar.gz
tar xzf libffi-3.2.1.tar.gz
cd libffi-3.2.1
./configure --disable-docs
make
make install

Build Python3.7.3:

wget https://www.python.org/ftp/python/3.7.2/Python-3.7.2.tgz
tar xzf Python-3.7.2.tgz && 
cd Python-3.7.2
export LD_LIBRARY_PATH=/usr/local/lib && \
export LD_RUN_PATH=/usr/local/lib && \
./configure --enable-optimizations --prefix=/usr/ --with-ensurepip=install --enable-shared LDFLAGS="-L/usr/local/lib" CPPFLAGS="-I /usr/local/lib/libffi-3.2.1/include"
make
make install
Amir Rossert
  • 1,003
  • 2
  • 13
  • 33
  • note that this command: `./configure --disable-docs` will raise WARNING, you need to download higher version of `libffi` to pass `--disable-docs`. – donto Feb 01 '21 at 14:03
2

This is my solution on debian 6.0.60, according to Amir Rossert's solution, thanks a lot!

(1) Install libffi

tar zxf libffi-3.3.tar.gz
cd libffi-3.3
./configure
make
make install

(2) Install Python 3.8

tar zxf Python-3.8.5.tgz
cd Python-3.8.5
export LD_LIBRARY_PATH=/usr/local/lib && \
export LD_RUN_PATH=/usr/local/lib && \
./configure --prefix=/usr/local/python38 --with-openssl=/usr/local/openssl111 --enable-shared --enable-optimizations --with-system-ffi=/usr/local/lib/
make
make install

ln -s /usr/local/python38/bin/python3 /usr/local/bin/python3
ln -s /usr/local/python38/bin/pip3 /usr/local/bin/pip3

touch /etc/ld.so.conf/python38.conf
echo "/usr/local/python38/lib" > /etc/ld.so.conf/python38.conf
ldconfig

Ok, it works well.

hluan
  • 21
  • 1
1

The problem on Ubuntu 10.04 is the Cflags in libffi.pc from libffi-dev. The following fixes the issue without upgrading the libffi package :

$ sed -i 's/Cflags:.*/Cflags: -I${includedir}\/x86_64-linux-gnu/' /usr/lib/pkgconfig/libffi.pc

The following Dockerfile builds Python 3.9.7 on Ubuntu 10.04:

FROM ubuntu:10.04

ARG PYVER=3.9.7

# Change to old-releases
RUN sed -i 's/http:\/\/archive.ubuntu.com\//http:\/\/old-releases.ubuntu.com\//' /etc/apt/sources.list

RUN apt-get update && DEBIAN_FRONTEND=noninteractive apt-get install -y \
    libreadline-dev libssl-dev zlib1g-dev libffi-dev \
    pkg-config build-essential

# Fix broken pkg-config for libffi (used by ctypes)
RUN sed -i 's/Cflags:.*/Cflags: -I${includedir}\/x86_64-linux-gnu/' /usr/lib/pkgconfig/libffi.pc

ADD https://www.python.org/ftp/python/$PYVER/Python-$PYVER.tgz /app/Python-$PYVER.tgz
RUN cd /app && tar zxvf Python-$PYVER.tgz && cd Python-$PYVER && \
    ./configure --prefix /opt/python3 && make && make install
lmunch
  • 11
  • 2