I develop Python application with support for different Python versions. I use pyenv
to have multiple Python versions on my system (Linux, if that matters) and to switch between them.
Now I also need to build Python packages with extensions, as not all of them yet publish wheels for the recent Python version. And my system has development headers which is guaranteed to work with only one Python version (package python3-dev
, which, on my system, serves Python 3.10.6). So attempts to build (some) packages in e.g. Python 3.12 pyenv
environment fail.
Ideally, I want to be able to say:
$ pyenv use 3.12
$ pip wheel <package> # <-- and GCC is using headers for Python 3.12, not for the version for which the development headers are installed system-wide
Is this a reasonable expectation? How can it be achieved?
One option which certainly satisfies this (whether it fits your workflow is a separate question) is to use docker containers.
For python, this is relatively easy:
Drops you into a bash shell where
python
is python 3.10, gcc is installed, and the python headers are present (trypython-config --includes
to check).To keep your changes, you can commit the container, or you can move the 'setup' to a single dockerfile and parametrize the base:
Now you can build multiple images by supplying
$version
at build time:Docker will pull down the relevant base container and build your image on top of it.
This is possible and easy in this case, because python provides a set of high quality docker images for every version you might care about.