当前报错的环境
- Python 3.9.19
- twine==1.15.0
本地~/.pypirc
已正确配置了用户名和密码,用在pypi.org注册:
[pypi]
username = skylerhu
password = ${password}
执行 python setup.py sdist upload -r pypi
打包上传到仓库报错。
在不久之前同样的环境,执行是成功过的。
错误1
执行 python setup.py sdist upload -r pypi
Submitting dist/py-enum-1.1.0.tar.gz to https://upload.pypi.org/legacy/
Upload failed (403): Username/Password authentication is no longer supported. Migrate to API Tokens or Trusted Publishers instead. See https://pypi.org/help/#apitoken and https://pypi.org/help/#trusted-publishers
error: Upload failed (403): Username/Password authentication is no longer supported. Migrate to API Tokens or Trusted Publishers instead. See https://pypi.org/help/#apitoken and https://pypi.org/help/#trusted-publishers
提示用户名、密码的鉴权方式已经不在支持,说是需要创建API Tokens
上传包。
根据提示,到用户管理页面 https://pypi.org/manage/account/ 找到配置,按照提示新增了API token.
需要注意的是创建Token后记得复制保存Token值,一般都是以pypi-
开头,复制值后按照提示配置本机.pypirc
如下所示,其中__token__
是固定写死的:
[pypi]
username = __token__
password = pypi-xxxx
参考: https://pypi.org/help/#apitoken
错误2
再次执行报错:
Upload failed (400): Invalid value for blake2_256_digest. Error: Use a valid, hex-encoded, BLAKE2 message digest.
error: Upload failed (400): Invalid value for blake2_256_digest. Error: Use a valid, hex-encoded, BLAKE2 message digest.
原因是distutils
(python setup.py工具)已弃用,不应再使用,请使用twine
将软件上传到 PyPI。
参考:
- https://bugs.python.org/issue45590
- https://github.com/pypa/distutils/issues/25
- https://blog.ganssle.io/articles/2021/10/setup-py-deprecated.html
错误3
改用twine执行 twine upload -r pypi dist/py-enum-xxx.tar.gz
还是报错:
Uploading distributions to https://upload.pypi.org/legacy/
Traceback (most recent call last):
File "./py-enum/.env3.9/bin/twine", line 8, in <module>
sys.exit(main())
File "./py-enum/.env3.9/lib/python3.9/site-packages/twine/__main__.py", line 28, in main
return dispatch(sys.argv[1:])
File "./py-enum/.env3.9/lib/python3.9/site-packages/twine/cli.py", line 77, in dispatch
return main(args.args)
File "./py-enum/.env3.9/lib/python3.9/site-packages/twine/commands/upload.py", line 137, in main
return upload(upload_settings, args.dists)
File "./py-enum/.env3.9/lib/python3.9/site-packages/twine/commands/upload.py", line 65, in upload
repository = upload_settings.create_repository()
File "./py-enum/.env3.9/lib/python3.9/site-packages/twine/settings.py", line 288, in create_repository
repo = repository.Repository(
File "./py-enum/.env3.9/lib/python3.9/site-packages/twine/repository.py", line 59, in __init__
self.session.mount(scheme, self._make_adapter_with_retries())
File "./py-enum/.env3.9/lib/python3.9/site-packages/twine/repository.py", line 65, in _make_adapter_with_retries
retry = util.Retry(
TypeError: __init__() got an unexpected keyword argument 'method_whitelist'
查找一些资料后,通过pip install -U twine
解决。
执行成功的版本是:twine==5.0.0
解决以上所有错误后,终于成功将包上传到了仓库。