如何在Python 2.7中设置HTTP代理?


74

我正在尝试运行安装pip的脚本:get-pip.py,并且由于我的网络位于HTTP代理后面而导致连接超时。有什么方法可以在Python 2.7安装中配置HTTP代理以安装我要安装的内容?

注意:我正在使用Windows。以下是我遇到的错误:

C:\SetupFiles>python get-pip.py
Downloading/unpacking pip
  Cannot fetch index base URL http://pypi.python.org/simple/
  Could not find any downloads that satisfy the requirement pip
No distributions at all found for pip

Answers:


137

似乎get-pip.py已更新为使用环境变量http_proxyhttps_proxy

视窗:

set http_proxy=http://proxy.myproxy.com
set https_proxy=https://proxy.myproxy.com
python get-pip.py

Linux / OS X:

export http_proxy=http://proxy.myproxy.com
export https_proxy=https://proxy.myproxy.com
sudo -E python get-pip.py

但是,如果这仍然无法解决问题,您始终可以easy_install通过设置相同的环境变量,使用setuptools '通过代理安装pip 。

视窗:

set http_proxy=http://proxy.myproxy.com
set https_proxy=https://proxy.myproxy.com
easy_install pip

Linux / OS X:

export http_proxy=http://proxy.myproxy.com
export https_proxy=https://proxy.myproxy.com
sudo -E easy_install pip

然后,在安装后,使用:

pip install --proxy="user:password@server:port" packagename

pip手册页中

--proxy让
pip使用代理服务器访问站点。可以使用“ user:password@proxy.server:port”符号指定。如果密码遗漏,pip会要求您输入密码。


您的答案似乎是假设已经安装了pip,但我尚未成功安装pip。仪器说要运行get-pip.py脚本,但是我遇到了连接超时问题,这是我遇到的麻烦。
罗兰多2012年

@Damascusi,您介意告诉我那个env var是否适用于get-pip脚本,以便我可以适当地更新答案吗?
本·伯恩斯

@BenBurns您的解决方案需要安装easy_install,但是我也需要安装代理才能安装...
youR.Fate 2014年

我厌倦了使用http_proxy和https_proxy设置的安装,但是(407) Proxyauthentifizierung erforderlich由于我的代理使用用户名和密码,因此出现了错误。我尝试设置http_proxyhttps_proxy使用username:password@proxy表示法,但仍然出现错误。
youR.Fate 2014年

1
我使用代理后面的get-pip.py成功安装了-即使是从跳转服务器到服务器的ssh隧道。export https_proxy=https://localhost:18080 ; export http_proxy=http://localhost:18080 ; export ftp_proxy=$http_proxy然后python get-pip.py
sastorsl 2014年

10

在我的网络上,仅设置http_proxy对我不起作用。以下几点是相关的。

1当执行sudo时,不会为用户设置http_proxy-要保留它,请执行以下操作:

sudo -E yourcommand

首先安装cntlm本地代理,使安装工作正常。这里的说明是简洁的:http : //www.leg.uct.ac.za/howtos/use-isa-proxies

您应该使用域用户名代替学生号

2要使用cntlm本地代理,请执行:

pip install --proxy localhost:3128 pygments

5

您几乎可以按照第一个答案中的说明安装pip(或任何其他软件包)easy_install。但是,您也将需要HTTPS代理。命令的完整顺序为:

set http_proxy=http://proxy.myproxy.com
set https_proxy=http://proxy.myproxy.com
easy_install pip

您可能还需要向代理添加端口,例如 http{s}_proxy=http://proxy.myproxy.com:8080



1

为了在代理后面安装带有get-pip.py的pip,我执行了以下步骤。我的服务器甚至位于跳转服务器之后。

从跳转服务器:

ssh -R 18080:proxy-server:8080 my-python-server

在“ python-server”上

export https_proxy=https://localhost:18080 ; export http_proxy=http://localhost:18080 ; export ftp_proxy=$http_proxy
python get-pip.py

成功。


1
cd C:\Python34\Scripts

set HTTP_PROXY= DOMAIN\User_Name:Passw0rd123@PROXY_SERVER_NAME_OR_IP:PORT#

set HTTP_PROXY= DOMAIN\User_Name:Passw0rd123@PROXY_SERVER_NAME_OR_IP:PORT#

pip.exe install PackageName
By using our site, you acknowledge that you have read and understand our Cookie Policy and Privacy Policy.
Licensed under cc by-sa 3.0 with attribution required.