从GitLab上传包到PyPI遇422 Unprocessable Entity错误求助
GitLab PyPI仓库上传报错HTTPError: 422排查
问题描述
首次尝试按GitLab官方教程将项目包上传至GitLab PyPI仓库,执行命令:
python3 -m twine upload --repository gitlab dist/*
控制台返回错误:
HTTPError: 422 Unprocessable Entity from https://gitlab.com/FiiireFlyyy/firelib
配置文件与代码
.pypirc配置
[distutils] index-servers = gitlab [gitlab] repository = https://gitlab.com/FiiireFlyyy/firelib username = my_token password = my_token_password
setup.py内容
# Always prefer setuptools over distutils from setuptools import setup, find_packages # To use a consistent encoding from codecs import open from os import path # The directory containing this file HERE = path.abspath(path.dirname(__file__)) # Get the long description from the README file with open(path.join(HERE, 'README.md'), encoding='utf-8') as f: long_description = f.read() # This call to setup() does all the work setup( name="firelib", version="0.1.0", description="Demo library", long_description=long_description, long_description_content_type="text/markdown", url="https://gitlab.com/FiiireFlyyy/firelib", author="Willy Lutz", author_email="myemail0willy@gmail.com", license="no licence", classifiers=[ "Intended Audience :: Developers", "License :: OSI Approved :: no licence", "Programming Language :: Python", "Programming Language :: Python :: 3", "Programming Language :: Python :: 3.6", "Programming Language :: Python :: 3.7", "Programming Language :: Python :: 3.8", "Programming Language :: Python :: 3.9", "Operating System :: OS Independent" ], packages=["firelib"], include_package_data=True, install_requires=["os", "shutil"] )
目录结构
firelib (pycharm project folder) |-----dist (directory) |-----firelib (package) |-----firelib.egg-info (directory) |-----venv |-----LICENCE |-----README |-----setup.py |-----requirements.txt
排查与修复方案
仓库地址错误:GitLab PyPI仓库的上传地址不是项目主页,需替换为API路径。两种格式可选:
- 用项目ID:
https://gitlab.com/api/v4/projects/{你的项目ID}/packages/pypi(项目ID可在GitLab项目主页的"设置"->"通用"中找到) - 用编码后的项目路径:
https://gitlab.com/api/v4/projects/FiiireFlyyy%2Ffirelib/packages/pypi(注意将路径中的/替换为%2F)
修改.pypirc中的repository字段为上述正确地址。
- 用项目ID:
认证配置错误:GitLab上传PyPI包需使用个人访问令牌(PAT),令牌需开启
write_package_registry权限。配置时:username必须填__token__(固定值)password填你的PAT内容,而非自定义的用户名密码
License分类器无效:
License :: OSI Approved :: no licence不符合标准,OSI Approved分类仅支持官方认可的许可证。若无公开许可证,可修改为:license="Proprietary", classifiers=[ # ... 其他分类器保留 "License :: Other/Proprietary License", # ... ]或直接移除
License :: OSI Approved :: no licence这一行。标准库依赖冗余:
os和shutil是Python内置标准库,无需加入install_requires,移除后可避免校验失败:install_requires=[]README文件不匹配:setup.py中读取的是
README.md,但目录中只有README文件,需将文件名改为README.md,或修改setup.py中的读取路径:with open(path.join(HERE, 'README'), encoding='utf-8') as f:
内容的提问来源于stack exchange,提问作者Willy Lutz
相关产品推荐
相关产品推荐

