要将Python包上传到PyPI,需要按照以下步骤操作:
1. 准备工作
- 注册PyPI账号:在pypi.org注册账号
-
安装必要工具:
pip install build twine
2. 项目配置
确保项目包含以下文件:
-
pyproject.toml(推荐)或setup.py README.md-
LICENSE文件 - 包的源代码目录
3. 构建包
在项目根目录运行:
python -m build
这会生成 dist/ 目录,包含:
- 源码包(.tar.gz)
- wheel包(.whl)
4. 上传到PyPI
twine upload dist/*
需要输入PyPI账号和API key进行认证。
https://pypi.org/manage/account/token/
注意事项:
- 确保包名在PyPI上唯一
- 版本号遵循语义化版本规范
- 上传前最好在测试仓库(test.pypi.org)测试
这样用户就能像安装其他Python包一样使用pip install命令了。
C:\Users\jbcod>twine upload D:\WORK\WORKSPACE_PY\CoPaw\dist\*
D:\WORK\python\Lib\site-packages\requests\__init__.py:113: RequestsDependencyWarning: urllib3 (2.5.0) or chardet (6.0.0.post1)/charset_normalizer (3.4.4) doesn't match a supported version!
warnings.warn(
Uploading distributions to https://upload.pypi.org/legacy/
WARNING This environment is not supported for trusted publishing
Enter your API token:
Uploading qzclaw-0.0.8.post5-py3-none-any.whl
100% ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━ 8.4/8.4 MB • 00:05 • 1.9 MB/s
Uploading qzclaw-0.0.8.post5.tar.gz
100% ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━ 8.1/8.1 MB • 00:03 • 2.4 MB/s
View at:
https://pypi.org/project/qzclaw/0.0.8.post5/
5. 验证
上传成功后,用户就可以通过 pip install 你的包名 来安装了。如果国内镜像没有同步过来,加上 --index-url https://pypi.org/simple/
pip install --index-url https://pypi.org/simple/ qzclaw
