private PyPI的pip.conf中的凭据

2024-06-15 09:08:42 发布

您现在位置:Python中文网/ 问答频道 /正文

我有一个私人的PyPI存储库。是否有任何方法可以将凭据存储在类似于.pypircpip.conf中?

我的意思是。当前在.pypirc中,您可以拥有这样的配置:

[distutils]
index-servers = custom

[custom]
repository: https://pypi.example.com
username: johndoe
password: changeme

根据我的发现,你可以输入pip.conf

[global]
index = https://username:password@pypi.example.com/pypi
index-url = https://username:password@pypi.example.com/simple
cert = /etc/ssl/certs/ca-certificates.crt

但在这里我看到两个问题:

  1. 对于每个url,每次都需要指定相同的用户名和密码。
  2. 用户名和密码在日志中可见,因为它们是url的一部分。

有没有办法将用户名和密码存储在url之外?


Tags: piphttpspypicomurl密码indexexample
2条回答

您可以将Pip的凭据存储在~/.netrc中,如下所示:

machine pypi.example.com
    login johndoe
    password changeme

Pip在访问https://pypi.example.com时将使用这些凭据,但不会记录它们。您必须单独指定索引服务器(如问题中的pip.conf)。

注意~/.netrc必须由用户拥有pip执行为。它也不能被任何其他用户读取。无效文件将被忽略。您可以确保权限正确,如下所示:

chown $USER ~/.netrc
chmod 0600 ~/.netrc

此权限检查在Python3.4之前不适用,但无论如何都是一个好主意。

内部Pip在发出HTTP请求时使用requests。请求使用标准库netrc模块读取文件,因此字符集仅限于ASCII子集。

如何将用户名/密码存储为环境变量

export username=username
export password=password

在pip.conf中这样引用它们:

[global]
index = https://$username:$password@pypi.example.com/pypi
index-url = https://$username:$password@pypi.example.com/simple
cert = /etc/ssl/certs/ca-certificates.crt

我使用Gitlab CI的秘密变量来存储凭据。检查CI工具中是否存在等效项。

相关问题 更多 >