You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

PyPi包新版本发布遇阻:旧命令失效及API令牌认证问题求助

发布PyPI包时的常见问题与解决方法

一、旧构建命令失效与pyproject.toml语法错误

更新源码准备发布后,找不到适配pyproject.toml的新版指南,多数资料还在提已淘汰的setup.py。在Windows+VSCode环境下,旧命令py setup.py sdist bdist_wheel已经失效,要是直接运行pyproject.toml还会弹出语法错误:

File "C:\Users\generic\pyproject.toml", line 3
    build-backend = "hatchling.build"
   ^
SyntaxError: cannot assign to operator

我的pyproject.toml是标准通用配置:

[build-system]
requires = ["hatchling"]
build-backend = "hatchling.build"

[project]
name = "example_package_YOUR_USERNAME_HERE"
version = "0.0.2"
authors = [
  { name="Example Author", email="author@example.com" },
]
description = "A small example package"
readme = "README.md"
requires-python = ">=3.7"
classifiers = [
    "Programming Language :: Python :: 3",
    "License :: OSI Approved :: MIT License",
    "Operating System :: OS Independent",
]

[project.urls]
"Homepage" = "https://github.com/pypa/sampleproject"
"Bug Tracker" = "https://github.com/pypa/sampleproject/issues"

解决方法

pyproject.toml是配置文件,不能直接当成脚本执行。替代旧setup.py构建命令的正确方式是用官方的build工具:

  1. 先安装build包:
    pip install build
    
  2. 执行构建命令,自动读取pyproject.toml配置生成发布包:
    python -m build
    
    这个命令会生成和旧命令一样的sdist源码包和bdist_wheel wheel包。

二、API令牌上传403权限错误

新生成的PyPI API令牌上传时返回403禁止错误:

ERROR    HTTPError: 403 Forbidden from https://test.pypi.org/legacy/
         Invalid or non-existent authentication information. See https://test.pypi.org/help/#invalid-auth for more information.

解决方法

  1. 区分测试/正式环境令牌:Test PyPI和正式PyPI的令牌不通用,确保用的是对应环境生成的令牌。
  2. 用twine工具上传:建议用官方推荐的twine工具,不要用旧的上传方式:
    • 先安装twine:
      pip install twine
      
    • 上传到Test PyPI:
      twine upload --repository testpypi dist/*
      
    • 执行后输入用户名__token__,密码填你生成的完整API令牌(不要加引号)。
  3. 配置文件简化操作:可以在用户目录下创建.pypirc文件(Windows路径是C:\Users\<你的用户名>\.pypirc),内容如下:
    [testpypi]
    username = __token__
    password = 你的Test PyPI令牌内容
    
    之后再执行上传命令就不用每次输入密码了。
  4. 检查令牌权限:生成令牌时要确保勾选了Upload package权限,只读权限会导致上传失败。

内容的提问来源于stack exchange,提问作者corvusMidnight

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.14 07:50:28