Python 如何让 pip 在代理服务器后面工作

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/19080352/
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 12:47:02  来源:igfitidea点击:

How to get pip to work behind a proxy server

pythonproxypip

提问by Annihilator8080

I am trying to use python package manager pip to install a package and it's dependencies from the internet. However I am behind a proxy in my college and have already set the http_proxyenvironment variable. But when I try to install a package like this:

我正在尝试使用 python 包管理器 pip 来安装一个包,它是来自互联网的依赖项。但是,我在我大学的代理后面,并且已经设置了http_proxy环境变量。但是当我尝试安装这样的软件包时:

pip install TwitterApi

I get this error in the log file:

我在日志文件中收到此错误:

Getting page http://pypi.python.org/simple/TwitterApi
Could not fetch URL http://pypi.python.org/simple/TwitterApi: <urlopen error [Errno 111] Connection refused>
Will skip URL http://pypi.python.org/simple/TwitterApi when looking for download links for TwitterApi
Getting page http://pypi.python.org/simple/
Could not fetch URL http://pypi.python.org/simple/: <urlopen error [Errno 111] Connection refused>

I even tried setting my proxy variable explicitly like this:

我什至尝试像这样显式设置我的代理变量:

pip install --proxy http://user:password@proxyserver:port TwitterApi

But I still get the same error. How do I get pip to work behind a proxy server.

但我仍然得到同样的错误。如何让 pip 在代理服务器后面工作。

回答by svvac

The pip's proxy parameter is, according to pip --help, in the form scheme://[user:passwd@]proxy.server:port

pip 的代理参数是,根据pip --help,在形式scheme://[user:passwd@]proxy.server:port

You should use the following:

您应该使用以下内容:

pip install --proxy http://user:password@proxyserver:port TwitterApi

Also, the HTTP_PROXYenv var should be respected.

此外,HTTP_PROXY应该尊重 env var。

Note that in earlier versions (couldn't track down the change in the code, sorry, but the doc was updated here), you had to leave the scheme://part out for it to work, i.e. pip install --proxy user:password@proxyserver:port

请注意,在早期版本中(无法追踪代码中的更改,抱歉,但文档已在此处更新),您必须将scheme://部分保留下来才能使其正常工作,即pip install --proxy user:password@proxyserver:port

回答by petre

At least for pip 1.3.1, it honors the http_proxy and https_proxy environment variables. Make sure you define both, as it will access the PYPI index using https.

至少对于 pip 1.3.1,它支持 http_proxy 和 https_proxy 环境变量。确保您定义了两者,因为它将使用 https 访问 PYPI 索引。

export https_proxy="http://<proxy.server>:<port>"
pip install TwitterApi

回答by Tudor Leustean

Old thread, I know, but for future reference, the --proxy option is now passed with an "="

我知道旧线程,但为了将来参考, --proxy 选项现在通过“=”传递

Example:

例子:

$ sudo pip install --proxy=http://yourproxy:yourport package_name

回答by Ruben

at least pip3 also works without "=", however, instead of "http" you might need "https"

至少 pip3 也可以在没有“=”的情况下工作,但是,您可能需要“https”而不是“http”

Final command, which worked for me:

最终命令,对我有用:

sudo pip3 install --proxy https://{proxy}:{port} {BINARY}

回答by Karthik C

First Try to set proxy using the following command

首先尝试使用以下命令设置代理

SET HTTPS_PROXY=http://proxy.***.com:PORT#

Then Try using the command

然后尝试使用命令

pip install ModuleName

回答by Gary Mendonca

On Ubuntu, you can set proxy by using

在 Ubuntu 上,您可以使用设置代理

export http_proxy=http://username:password@proxy:port
export https_proxy=http://username:password@proxy:port

or if you are having SOCKS error use

或者如果您遇到 SOCKS 错误,请使用

export all_proxy=http://username:password@proxy:port

Then run pip

然后运行pip

sudo -E pip3 install {packageName}