By a complex package, I mean a package with pure Python code as well as code in other languages like C, Fortran, etc.
Note: I am assuming that your setup.py already works, and I will show you how to create a manylinux Python wheel. You will need Docker for this.
To create a distribution of your Python package with statically linked libraries (i.e., a self-contained package), you need to use a Linux distribution that provides all necessary dependencies. This is where manylinux comes in handy.
To get started, pull the container where you will install your Python package:
docker pull quay.io/pypa/manylinux2010_x86_64Then, log into it:
docker run -ti -v ~/MyPackage:/io quay.io/pypa/manylinux2010_x86_64 /bin/bashNotes:
-
Make sure to install the correct version of
numpyto ensure compatibility with thesetuptoolsversion:/opt/python/cp37-cp37m/bin/python -m pip install numpy /opt/python/cp39-cp39/bin/python -m pip install numpy==1.22
-
Navigate to the
/iodirectory and clone your project repository:cd /io/ git clone https://github.com/user.or.org/MyPackage.git -
Enter the repository folder and run the
setup.pyscript with thebdist_wheelandsdistoptions:cd MyPackage/ /opt/python/cp37-cp37m/bin/python setup.py bdist_wheel sdist /opt/python/cp39-cp39/bin/python setup.py bdist_wheel sdist cd dist/
-
To create a Linux-agnostic wheel, use the
auditwheeltool:auditwheel repair MyPackage-x.y.z-cp37-cp37m-linux_x86_64.whl auditwheel repair MyPackage-x.y.z-cp39-cp39-linux_x86_64.whl
-
The final wheel file will be in the
wheelhousedirectory:cd wheelhouse/ ls MyPackage-x.y.z-cp37-cp37m-manylinux_2_12_x86_64.manylinux2010_x86_64.whl ls MyPackage-x.y.z-cp39-cp39-manylinux_2_12_x86_64.manylinux2010_x86_64.whl -
Finally, copy the wheel file from the Docker container to your local host:
First, get your container ID:
docker ps
The container ID should look something like
73dc6928ec89. Next, copy the wheel files you want:docker cp 73dc6928ec89:/io/MyPackage/dist/wheelhouse/MyPackage-x.y.z-cp37-cp37m-manylinux_2_12_x86_64.manylinux2010_x86_64.whl . docker cp 73dc6928ec89:/io/MyPackage/dist/wheelhouse/MyPackage-x.y.z-cp39-cp39-manylinux_2_12_x86_64.manylinux2010_x86_64.whl . docker cp 73dc6928ec89:/io/MyPackage/dist/MyPackage-x.y.z.tar.gz .
Use twine to publish MyPackage to https://pypi.org (if you don't have twine installed, simply run pip install twine):
twine upload dist/*Now, MyPackage can be easily installed with:
pip install MyPackage