# PyPI (Python Package Index) Python 包索引(PyPI)是 Python 编程语言的软件存储库。南大源采用了阿里云,腾讯云和官方源的混合代理形式。 ## pip 配置指南 ### 临时使用 ```shell pip install -i https://repo.nju.edu.cn/repository/pypi/simple [packageName] ``` 使用此命令可以临时使用南大源下载指定包。 ### 设置为默认源 如果想将南大源设置为默认源,则可能需要先更新 pip 版本: ```shell pip install pip -U # 更新 pip 版本 pip config set global.index-url https://repo.nju.edu.cn/repository/pypi/simple # 修改源 ``` ### 超时说明 由于在第一次用户请求的时候,服务器会从远程(源)服务器获取文件,因此第一次请求部分**大体积包**(如 tensorflow )时,可能会出现以下错误: ```text WARNING: Retrying (Retry(total=4, connect=None, read=None, redirect=None, status=None)) after connection broken by 'ReadTimeoutError("HTTPSConnectionPool(host='repo.nju.edu.cn', port=443): Read timed out. (read timeout=15)")': [uri] WARNING: Retrying (Retry(total=3, connect=None, read=None, redirect=None, status=None)) after connection broken by 'ReadTimeoutError("HTTPSConnectionPool(host='repo.nju.edu.cn', port=443): Read timed out. (read timeout=15)")': [uri] WARNING: Retrying (Retry(total=2, connect=None, read=None, redirect=None, status=None)) after connection broken by 'ReadTimeoutError("HTTPSConnectionPool(host='repo.nju.edu.cn', port=443): Read timed out. (read timeout=15)")': [uri] WARNING: Retrying (Retry(total=1, connect=None, read=None, redirect=None, status=None)) after connection broken by 'ReadTimeoutError("HTTPSConnectionPool(host='repo.nju.edu.cn', port=443): Read timed out. (read timeout=15)")': [uri] WARNING: Retrying (Retry(total=0, connect=None, read=None, redirect=None, status=None)) after connection broken by 'ReadTimeoutError("HTTPSConnectionPool(host='repo.nju.edu.cn', port=443): Read timed out. (read timeout=15)")': [uri] ERROR: Could not install packages due to an OSError: HTTPSConnectionPool(host='repo.nju.edu.cn', port=443): Max retries exceeded with url: [url] (Caused by ReadTimeoutError("HTTPSConnectionPool(host='repo.nju.edu.cn', port=443): Read timed out. (read timeout=15)")) ``` 这是由于初次请求,服务器去其他站下载的原因,稍后一会(一般建议2分钟左右)重新执行该命令即可。 --- ## pypi 私服 南京大学 pypi 仓库同时提供 **pip 包托管服务**,本校师生可以将自己编写的 python 脚本打包后发布到仓库中让其他人使用。此仓库文件**不会同步到其他镜像源**。 ### 从仓库获取文件 将上面提到的仓库地址改为 `https://repo.nju.edu.cn/repository/pypi-nju/simple` 即可。 使用此仓库请注意: - 此仓库会优先从官方库和其他源中获取包,如果你发布了同名包,会被其他来源的同名包覆盖,**因此请不要也禁止发布同名包或者名称相近包**; - 当仓库网络出现问题或者上游源出现问题时,**可能会导致你下载到仓库其他人发布的同名包**,这可能不是你想要的,甚至有风险(pypi 仓库则没有此问题); - 如果发现和上游仓库**同名包**,请[填写统计表](https://table.nju.edu.cn/dtable/forms/818ab7f9-6525-47d5-be85-2f527e7ae8d3/),管理员将会定期删除; - 如果发现**恶意同名包或者类似包**,请[填写统计表](https://table.nju.edu.cn/dtable/forms/818ab7f9-6525-47d5-be85-2f527e7ae8d3/),管理员将核实后采取措施。 - 当由于毕业等原因导致统一身份认证账号过期时,你将不能向该仓库中推送新的包或者已有包的更新,正常情况下你已经提交的包也不会被删除。 ### 向仓库提交文件 - linux 用户打开终端并输入以下命令: ```shell vim ~/.pypirc ``` - windows 用户打开 powershell 或者 pwsh, 输入以下命令: ```shell cd ~ notepad .pypirc ``` 在打开的文件中写入以下内容: ```ini [distutils] index-servers = nju [nju] repository: https://repo.nju.edu.cn/repository/pypi-releases/ username: [学工号] password: [统一身份认证密码] ``` 在项目根路径执行: ```shell python setup.py check python setup.py sdist bdist_wheel upload -r nju ``` 更多关于 python 打包相关的内容,请参考[相关链接](https://packaging.python.org/tutorials/packaging-projects/)。 ### 注意 - 一经发布,文件**不允许修改和删除**; - 发布过程中你的用户信息会被记录,因此请勿滥用; - 建议为你的包起一个合适的名字和有规则的版本号; - 如果想配置多个发布仓库,请[参考链接](https://packaging.python.org/specifications/pypirc/)。