Python 如何为每个配置文件配置 PIP 以使用代理(带身份验证)?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/43473041/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-19 23:06:35  来源:igfitidea点击:

How to configure PIP per config file to use a proxy (with authentification)?

pythonproxypipini

提问by Wlad

I used to set up environment evariables http_proxy and https_proxy (with user + password) in the past to use Pip (on Windows) behind a corporate proxy. But recently I needed to tell Pip to use a proxy without setting up environment variables as this conflicted with git configuration in combination with SSL Certificates which I get to work only by removing environment variables for proxy.

我过去常常设置环境变量 http_proxy 和 https_proxy(使用用户 + 密码)以在公司代理后面使用 Pip(在 Windows 上)。但是最近我需要告诉 Pip 在不设置环境变量的情况下使用代理,因为这与 git 配置与 SSL 证书相冲突,我只能通过删除代理的环境变量来工作。

Fortunately you can configure PIP with an pip.ini file as described here: https://pip.pypa.io/en/stable/user_guide/#config-file

幸运的是,您可以使用 pip.ini 文件配置 PIP,如下所述:https://pip.pypa.io/en/stable/user_guide/#config-file

The detailed answer to my own question follows below.

我自己的问题的详细答案如下。

回答by Wlad

Here are the steps how to configure proxy (with auth.) in pip's config file (pip.ini)

以下是如何在 pip 的配置文件 (pip.ini) 中配置代理(使用 auth.)的步骤

  1. (if it does not already exist) Create a folder named 'pip' and inside it a file named 'pip.ini' as described here: https://pip.pypa.io/en/stable/user_guide/#config-file(location an name may differ per platform - e.g. on Windows it's %APPDATA%\pip\pip.ini)
  2. edit pip.ini file and add

    [global]
    proxy = http://user:password@proxy_name:port
    
  3. That's it!
  1. (如果它不存在)创建一个名为“pip”的文件夹,并在其中创建一个名为“pip.ini”的文件,如下所述:https://pip.pypa.io/en/stable/user_guide/#config-file (位置名称可能因平台而异 - 例如在 Windows 上它是 %APPDATA%\pip\pip.ini)
  2. 编辑 pip.ini 文件并添加

    [global]
    proxy = http://user:password@proxy_name:port
    
  3. 就是这样!

Example for proxy with authentification (user + password):

带身份验证的代理示例(用户 + 密码):

proxy = http://butch:secret@proxyname:1234

代理 = http://butch:secret@proxyname:1234

proxyname can be an IP adress, too

proxyname 也可以是 IP 地址

Example for proxy without auth.:

没有身份验证的代理示例:

proxy = http://proxyname:1234

代理 = http://proxyname:1234

回答by crunchyminion

In order to add a proxy option in the terminal the following line solved the problem for me:

为了在终端中添加代理选项,以下行为我解决了问题:

pip install package_name_here --proxy https://user_name:password@proxyname:port

回答by Kishore Kolla

You need to set proxy option while installing the package. example:

您需要在安装软件包时设置代理选项。例子:

pip install --proxy userid:[email protected]:yourport