如何在本地计算机上发布适用于 Linux 的二进制 Python 轮子

Ero*_*mic 5 python pypi python-wheel

我有一个包含 C 扩展的包,我想将其上传到 pypi: https: //github.com/Erotemic/netharn

我使用命令在 Ubuntu 18.04 上构建了一个轮子python setup.py bdist_wheel --py-limited-api=cp36,但是当我使用 上传时twine upload --skip-existing dist/*,却收到错误消息,它有一个不受支持的平台标签:

HTTPError: 400 Client Error: Binary wheel 'netharn-0.0.4-cp36-abi3-linux_x86_64.whl' has an unsupported platform tag 'linux_x86_64'. for url: https://upload.pypi.org/legacy/
Run Code Online (Sandbox Code Playgroud)

经过一番搜索后,我发现PEP 513需要构建一个轮子来支持manylinux(又名Centos5): https: //github.com/pypa/manylinux

他们在这里提供了一个示例:https://github.com/pypa/python-manylinux-demo/blob/master/travis/build-wheels.sh

然而,我能找到的所有示例总是使用某种 CI 服务器构建二进制文件。如果可能的话,我希望能够在本地构建它们。我认为复制 docker 命令并将其构建在我自己的机器上的 docker 容器中应该很简单。但是,我遇到了问题。(我确保删除了存储库中任何现有的构建和 dist 目录)

我做的第一件事就是将自己转入交互式 docker 会话中,这样我就可以玩东西了。我选择了 x8_64 映像并将本地目录安装到/iodocker 计算机上的代码存储库。然后我开始了交互式 bash 会话。

REPO_DPATH=$HOME/code/netharn
DOCKER_IMAGE=quay.io/pypa/manylinux1_x86_64
PRE_CMD=""
# Interactive test
docker run -it --rm -v $REPO_DPATH:/io $DOCKER_IMAGE $PRE_CMD bash
Run Code Online (Sandbox Code Playgroud)

在 docker 内部,我首先想为 python36 构建一个轮子(实际上这是我目前唯一有兴趣支持的 Python)。

PYBIN=/opt/python/cp36-cp36m/bin/
Run Code Online (Sandbox Code Playgroud)

简单地安装我的requirements.txt似乎不起作用,所以我先手动安装了一些软件包。这样做之后(这imgaug是罪魁祸首,因为它依赖于特定的主版本),安装requirements.txt似乎有效。

    cd /io
    "${PYBIN}/pip" install opencv_python
    "${PYBIN}/pip" install Cython
    "${PYBIN}/pip" install pytest
    "${PYBIN}/pip" install -e git+https://github.com/aleju/imgaug.git@master#egg=imgaug
    "${PYBIN}/pip" install torch  # this is in the requirements.txt, but will cause problems later
    "${PYBIN}/pip" install -r requirements.txt
Run Code Online (Sandbox Code Playgroud)

然后,我运行wheel命令并将外部共享库捆绑到wheels中

"${PYBIN}/pip" wheel /io/ -w wheelhouse/
for whl in wheelhouse/*.whl; do
    auditwheel repair "$whl" -w /io/wheelhouse/
done
Run Code Online (Sandbox Code Playgroud)

最后一步是安装包并测试

    "${PYBIN}/pip" install netharn --no-index -f /io/wheelhouse

    (cd "$HOME"; "${PYBIN}/python" -m xdoctest netharn all)
Run Code Online (Sandbox Code Playgroud)

然而,当我测试它时,我得到了

ImportError: /opt/python/cp36-cp36m/lib/python3.6/site-packages/torch/_C.cpython-36m-x86_64-linux-gnu.so: ELF file OS ABI invalid
Run Code Online (Sandbox Code Playgroud)

我猜这是因为torch不支持Centos5。我不明白的是如何torchcpython-36m-x86_64-linux-gnu.so共享库上传到 pypi,但我遇到了问题?

Ero*_*mic 0

为了跟进这一点,实现这一目标的现代(截至 2023 年)方法是使用 cibuildwheel: https: //cibuildwheel.readthedocs.io/en/stable/

这在本地和 CI 实例上都很有效。我有几个项目使用这种方法来构建、测试和发布二进制轮子。

https://gitlab.kitware.com/computer-vision/kwimage_ext

https://github.com/Erotemic/vtool_ibeis_ext

https://github.com/Erotemic/pyflann_ibeis

https://github.com/Erotemic/pyhesaff

https://github.com/Erotemic/networkx_algo_common_subtree

这些都使用 CMake + scikit-build 来定义如何构建二进制文件,然后 cibuildwheel 在 pyproject.toml 中以最少的配置处理其余部分