Search code examples
pythondockerpython-wheelpython-extensions

How to build a python package with a c extension as a wheel for docker


I am trying to build a python package with a c extension to be used in a Docker container.

I create a python wheel as follows:

python install bdist_wheel

and the package looks (more or less) like

cpack
  __init__.py
  /cpp
  __init__.py
  c_extension.cpython-37m-x86_64_linux-gnu.so
 ...

and use the following Dockerfile.

FROM python:3.8-slim-buster

WORKDIR /home/user

COPY --chown=user:user requirements.txt /tmp/requirements.txt

RUN python -m venv user

RUN user/bin/pip install -r /tmp/requirements.txt 

RUN user/bin/pip install wheel
RUN user/bin/pip install my_c_package-0.0.1-py3-none-any.whl

USER user

...

When I install my my_c_package-0.0.1-py3-none-any.whl locally I have no issues, but on docker I get

import cpack
...
ModuleNofFoundError: No module named cpack.cpp.c_extension

As it works fine locally, it is not clear to me what is missing?


Solution

  • updating the extension to use python 3.8 in the build of the extension as indicated by phd in the comments ensures the extension is picked up correctly by python 3.8.