sto*_*lho 18 python config pip pypi
我有一个私有的PyPI存储库.有没有办法存储pip.conf类似的凭证.pypirc?
我的意思是说.目前.pypirc你可以有这样的配置:
[distutils]
index-servers = custom
[custom]
repository: https://pypi.example.com
username: johndoe
password: changeme
Run Code Online (Sandbox Code Playgroud)
从我发现你可以放入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
Run Code Online (Sandbox Code Playgroud)
但在这里我看到两个问题:
有没有办法在网址之外存储用户名和密码?
小智 12
您可以存储供Pip使用的凭据,~/.netrc如下所示:
machine pypi.example.com
login johndoe
password changeme
Run Code Online (Sandbox Code Playgroud)
点子将在访问时使用这些凭据,https://pypi.example.com但不会记录它们。您必须单独指定索引服务器(例如pip.conf在问题中)。
注意~/.netrc必须由用户拥有的pip执行方式为。同样,任何其他用户都不能读取它。无效文件将被静默忽略。您可以像这样确保权限正确:
machine pypi.example.com
login johndoe
password changeme
Run Code Online (Sandbox Code Playgroud)
此权限检查在Python 3.4之前不适用,但是在任何情况下都是一个好主意。
内部皮普使用请求 HTTP请求时。请求使用标准库netrc模块读取文件,因此字符集仅限于ASCII子集。
小智 8
如何将用户名/密码存储为环境变量,
export username=username
export password=password
Run Code Online (Sandbox Code Playgroud)
并在 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
Run Code Online (Sandbox Code Playgroud)
我使用 Gitlab CI 的秘密变量来存储凭据。检查 CI 工具中的等效项。
鉴于问题 4789仍然悬而未决,您可以在您的requirements.txt中使用以下方法:
--extra-index-url=https://${PYPI_USERNAME}:${PYPI_PASSWORD}@my.privatepypi.com
private-package==1.2.3
...
Run Code Online (Sandbox Code Playgroud)
如果您尝试设置pip install -r requirements.txt这些环境变量来运行,您会发现 pip 仍然要求提供凭据。这是因为 pip 并不像人们期望的那样对表达式进行插值${PYPI_USERNAME},而是对其进行 url 编码。本例中您的用户名和密码表示为https://%24%7BPYPI_USERNAME%7D:%24%7BPYPI_PASSWORD%7D@my.privatepypi.com
这里的工作,我承认应该有更好的方法,但我们可以将 pip 作为脚本运行:
python3 -m pip install -r requirements.txt
Run Code Online (Sandbox Code Playgroud)
来自男人:
-m module-name
Searches sys.path for the named module and runs the corresponding .py file as a script.
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
5649 次 |
| 最近记录: |