使用 TestPyPI¶
TestPyPI 是 Python 包索引 (PyPI) 的一个独立实例,它允许您试用分发工具和流程,而无需担心会影响真实的索引。TestPyPI 托管在 test.pypi.org
注册您的账户¶
由于 TestPyPI 拥有一个与实时 PyPI 独立的数据库,您将需要一个专门用于 TestPyPI 的独立用户账户。请访问 https://test.pypi.org/account/register/ 注册您的账户。
注意
TestPyPI 的数据库可能会定期修剪,因此用户账户被删除并不少见。
使用 Twine 和 TestPyPI¶
您可以通过指定 --repository 标志来使用 twine 将您的分发包上传到 TestPyPI
twine upload --repository testpypi dist/*
您可以通过访问 URL https://test.pypi.org/project/<sampleproject> 来查看您的包是否已成功上传,其中 sampleproject 是您上传的项目名称。您的项目可能需要一两分钟才能出现在网站上。
使用 pip 和 TestPyPI¶
您可以通过指定 --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 的规范。