How to set an HTTP proxy in Python 2.7?
我正在尝试运行安装pip的脚本:get-pip.py,并且由于我的网络位于HTTP代理后面而导致连接超时。 有什么方法可以在Python 2.7安装中配置HTTP代理以安装我要安装的内容?
注意:我正在使用Windows。 以下是我得到的错误:
1 2 3 4 5 | 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 |
看起来
视窗:
1 2 3 | set http_proxy=http://proxy.myproxy.com set https_proxy=https://proxy.myproxy.com python get-pip.py |
Linux / OS X:
1 2 3 | export http_proxy=http://proxy.myproxy.com export https_proxy=https://proxy.myproxy.com sudo -E python get-pip.py |
但是,如果这仍然无法解决问题,您始终可以通过设置相同的环境变量,使用setuptools的
视窗:
1 2 3 | set http_proxy=http://proxy.myproxy.com set https_proxy=https://proxy.myproxy.com easy_install pip |
Linux / OS X:
1 2 3 | export http_proxy=http://proxy.myproxy.com export https_proxy=https://proxy.myproxy.com sudo -E easy_install pip |
然后,在安装后,使用:
1 | pip install --proxy="user:password@server:port" packagename |
在pip手册页中:
--proxy
Have pip use a proxy server to access sites. This can be specified
using"user:[email protected]:port" notation. If the password
is left out, pip will ask for it.
在我的网络上,仅设置http_proxy对我不起作用。以下几点是相关的。
1当执行sudo时,不会为用户设置http_proxy-要保留它,请执行以下操作:
1 | sudo -E yourcommand |
我首先通过安装cntlm本地代理来使安装正常进行。这里的说明是简洁的:http://www.leg.uct.ac.za/howtos/use-isa-proxies
您应该使用域用户名代替学生号
2要使用cntlm本地代理,请执行:
1 | pip install --proxy localhost:3128 pygments |
您可以按照第一个答案中的说明,用
1 2 3 | set http_proxy=http://proxy.myproxy.com set https_proxy=http://proxy.myproxy.com easy_install pip |
您可能还需要向代理添加端口,例如
1 2 3 4 5 6 7 | 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 |
为了在代理后面安装带有get-pip.py的pip,我执行了以下步骤。我的服务器甚至位于跳转服务器之后。
从跳转服务器:
1 | ssh -R 18080:proxy-server:8080 my-python-server |
在" python-server"上
1 2 | export https_proxy=https://localhost:18080 ; export http_proxy=http://localhost:18080 ; export ftp_proxy=$http_proxy python get-pip.py |
成功。
您可以尝试从此处下载Windows pip二进制文件:http://www.lfd.uci.edu/~gohlke/pythonlibs/#pip。
有关使用pip下载其他模块的信息,请参见@Ben Burn的答案。