使用 TestPyPI#

TestPyPIPython 包索引 (PyPI) 的一个独立实例,它允许你试用分发工具和流程,而无需担心影响实际索引。TestPyPI 托管在 test.pypi.org

注册你的帐户#

由于 TestPyPI 拥有与实时 PyPI 分离的数据库,因此你需要一个专门用于 TestPyPI 的独立用户帐户。转到 https://test.pypi.org/account/register/ 注册你的帐户。

注意

TestPyPI 的数据库可能会定期清除,因此删除用户帐户并不罕见。

将 TestPyPI 与 Twine 一起使用#

你可以使用 twine 通过指定 --repository 标志将你的分发上传到 TestPyPI

twine upload --repository testpypi dist/*

你可以通过导航到 URL https://test.pypi.org/project/<sampleproject> 查看你的包是否已成功上传,其中 sampleproject 是你上传的项目名称。你的项目可能需要一两分钟才能出现在网站上。

将 TestPyPI 与 pip 一起使用#

你可以通过指定 --index-url 标志告诉 pip 从 TestPyPI 而不是 PyPI 下载包

python3 -m pip install --index-url https://test.pypi.org/simple/ your-package
py -m pip install --index-url https://test.pypi.org/simple/ your-package

如果你希望允许 pip 也从 PyPI 下载包,你可以指定 --extra-index-url 指向 PyPI。当你要测试的包有依赖项时,这很有用

python3 -m pip install --index-url https://test.pypi.org/simple/ --extra-index-url https://pypi.ac.cn/simple/ your-package
py -m pip install --index-url https://test.pypi.org/simple/ --extra-index-url https://pypi.ac.cn/simple/ your-package

.pypirc 中设置 TestPyPI#

如果你想避免每次都被提示输入用户名和密码,可以在 $HOME/.pypirc 中配置 TestPyPI

[testpypi]
username = __token__
password = <your TestPyPI API Token>

有关更多详细信息,请参阅 .pypirc规范