How to build a compiled module for multiple Python versions and platforms
Asked Answered
S

2

5

I have build a Python 3 module for my own process.

I use cython to compile and wrap C++ sources.

I have a Linux (Debian Jessie) machine with Python 3.4 and so cythonize make me a Processing.cpython-34m.so and copy it to /usr/local/lib/python3.4/dist-packages.

But when I use it on another machine which has python3.5, I have to recompile everything.

How can I build a Linux or pip package from my machine for all Python 3 version and multiple platforms (here, just Jessie and Stretch, which might be very closed indeed equal) ? Preferably without having to install all version of Python 3 on my machine.

Here is my setup.py file for cythonization :

from distutils.core import setup, Extension
from Cython.Build import cythonize

setup(ext_modules = cythonize(Extension(
            "MyProcessing",
            sources=["MyProcessing.pyx", "myprocess.cpp", "mythirdp.cpp"],
            language="c++", 
        )))

Thanks.

Schrader answered 14/3, 2018 at 13:43 Comment(1)
You may be interested in multibuild: github.com/matthew-brett/multibuildRibera
K
4

Make manylinux1 binary wheels that work across distributions - see PEP-513

It involves running a docker build on the official PyPa manylinux1 docker images that builds binary wheels for all python versions.

These wheels can be distributed on PyPi and are usable across distributions.

The constraint is that the build needs to be done in a Centos5 distribution which the manylinux1 image is based on for compatibility.

See PyPa's manylinux demo repository for an example.

Kellene answered 14/3, 2018 at 15:2 Comment(0)
F
3

Python wheels are tied to the python ABI. The ABI is stable within a minor release, which is why your wheels have 34 tag - the typical solution is to create a wheel for each minor version of python you want to support, so you'd need to install all those versions. For more see PEP 425

For python 3.2+ there is a definition of a 'stable ABI' that works across all versions. I'm not sure exactly what the limitations are and to my knowledge cython doesn't work with it, but I believe for a hand written extension you could create a wheel that works across all versions of python 3.

Away from cython, if you're primarily wrapping c-code another potential solution is CFFI. It does not link against libpython, so I think you can generate a wheel that is version independent. It's not something I've used but Armin Ronacher has a package that seems to directly address this case https://github.com/getsentry/milksnake

edit: make sure to read @danny's answer about manylinux, that's an important point I forgot

Factoring answered 14/3, 2018 at 14:29 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.