使用Github Actions多平台发布PyPI包遇重复上传问题求助
解决多平台PyPI自动发布的重复上传问题
1. 拆分任务分工,仅单个平台生成并上传源码包
源码包(sdist)是跨平台通用的,只需要生成上传一次。可以在GitHub Actions的配置里给不同平台的Job分配不同职责:
- 指定一个平台(比如ubuntu-latest)负责生成sdist+对应平台wheel并上传
- 其他平台(比如macos-latest)只生成自身平台的wheel并上传
示例release.yml配置片段:
jobs: build-ubuntu: runs-on: ubuntu-latest steps: - uses: actions/checkout@v4 - name: 配置Python环境 uses: actions/setup-python@v5 with: python-version: '3.x' - name: 安装依赖工具 run: | python -m pip install --upgrade pip pip install setuptools wheel twine - name: 构建源码包和Linux平台wheel run: python setup.py sdist bdist_wheel - name: 上传到PyPI env: TWINE_USERNAME: __token__ TWINE_PASSWORD: ${{ secrets.PYPI_API_TOKEN }} run: twine upload dist/* build-macos: runs-on: macos-latest steps: - uses: actions/checkout@v4 - name: 配置Python环境 uses: actions/setup-python@v5 with: python-version: '3.x' - name: 安装依赖工具 run: | python -m pip install --upgrade pip pip install setuptools wheel twine - name: 仅构建macOS平台wheel run: python setup.py bdist_wheel - name: 上传到PyPI env: TWINE_USERNAME: __token__ TWINE_PASSWORD: ${{ secrets.PYPI_API_TOKEN }} run: twine upload dist/*.whl
2. 使用twine的跳过参数自动忽略已上传文件
如果不想拆分任务,直接在所有平台的上传命令中添加--skip-existing参数,让twine自动跳过PyPI上已存在的文件,避免触发"文件已存在"错误。
修改上传命令为:
twine upload --skip-existing dist/*
这种方式下,每个平台都可以生成sdist和wheel,但上传时会自动跳过已经上传过的文件(比如第一个平台传完sdist后,第二个平台会跳过sdist,只传自己的wheel)。
3. 确保wheel包带有正确的平台标签
为了让pip install时能自动匹配对应平台的wheel,需要确保生成的wheel带有标准平台标签:
- Linux平台:使用
auditwheel工具修复wheel,生成符合manylinux标准的包pip install auditwheel auditwheel repair dist/*.whl --plat manylinux_2_17_x86_64 twine upload wheelhouse/*.whl - macOS平台:使用
delocate工具处理wheel,添加正确的macOS版本标签pip install delocate delocate-wheel dist/*.whl twine upload dist/*.whl
处理后的wheel会带有类似manylinux_2_17_x86_64、macosx_10_9_x86_64的标签,pip会自动根据用户系统匹配对应的安装包。
内容的提问来源于stack exchange,提问作者aaries
相关产品推荐
相关产品推荐

