如何在Python2.7中设置HTTP代理?

如何在Python2.7中设置HTTP代理?,python,Python,我正在尝试运行一个安装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/

我正在尝试运行一个安装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

看起来
get pip.py
已更新为使用环境变量
http\u proxy
https\u proxy

窗口:

set http_proxy=http://proxy.myproxy.com
set https_proxy=https://proxy.myproxy.com
python get-pip.py
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 python get-pip.py
export http_proxy=http://proxy.myproxy.com
export https_proxy=https://proxy.myproxy.com
sudo -E easy_install pip
但是,如果这仍然不适用于您,则始终可以通过设置相同的环境变量,使用“
easy\u install
”通过代理安装pip

窗口:

set http_proxy=http://proxy.myproxy.com
set https_proxy=https://proxy.myproxy.com
python get-pip.py
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 python get-pip.py
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使用代理服务器访问站点。这可以指定 使用“用户:password@proxy.server:port“符号。如果输入密码 如果被遗漏了,匹普会要求的


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

1执行sudo时,不会保留用户的http_代理设置-要保留它,请执行以下操作:

sudo -E yourcommand
我通过首先安装cntlm本地代理来完成安装。这里的说明很简洁:

而不是学生号,你应该把你的域名用户名

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

pip install --proxy localhost:3128 pygments

您可以使用
easy\u install
安装
pip
(或任何其他软件包),几乎如第一个答案中所述。但是,您也需要一个
HTTPS
代理。命令的完整顺序是:

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

您可能还希望向代理添加端口,例如
http{s}\u代理=http://proxy.myproxy.com:8080

您可以尝试从以下位置下载pip的Windows二进制文件:


有关使用pip下载其他模块的信息,请参见。

有关在代理后安装带有get-pip.py的pip的信息,我按照以下步骤进行了操作。我的服务器甚至落后于跳转服务器

从跳转服务器:

ssh -R 18080:proxy-server:8080 my-python-server
在“python服务器”上


成功。

您的回答似乎假设已经安装了pip,但我尚未成功安装pip。说明中说要运行get-pip.py脚本,但我遇到了连接超时,这正是我遇到的问题。@Damasusi,您介意告诉我该环境变量是否适用于get-pip脚本,以便我可以适当地更新答案吗?@BenBurns您的解决方案需要安装easy_install,然而,我也需要一个代理来安装它…我已经厌倦了安装它,设置了http_代理和https_代理,但是我得到了错误
(407)proxyAuthentifizierRung erforderlich
,因为我们的代理使用用户名和密码。我尝试使用
用户名设置
http\u代理
https\u代理
:password@proxy
notation,但我仍然遇到了错误。我成功地在代理后面安装了get-pip.py,即使使用ssh隧道从跳转服务器进入服务器<代码>导出https\u代理=https://localhost:18080 ; 导出http\U代理=http://localhost:18080 ; 导出ftp_proxy=$http_proxy然后
python get pip.py
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