How to set an HTTP proxy in Python 2.7?

Python

Python Problem Overview


I am trying to run a script that installs pip: get-pip.py and am getting a connection timeout due to my network being behind an HTTP proxy. Is there some way I could configure an HTTP proxy in my Python 2.7 installation to be able to install what I am trying to install?

Note: I am using Windows. Below is the error I am getting:

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

Python Solutions


Solution 1 - Python

It looks like get-pip.py has been updated to use the environment variables http_proxy and https_proxy.

Windows:

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

However if this still doesn't work for you, you can always install pip through a proxy using setuptools' easy_install by setting the same environment variables.

Windows:

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

Then once it's installed, use:

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

From the pip man page:

> --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.

Solution 2 - Python

On my network just setting http_proxy didn't work for me. The following points were relevant.

1 Setting http_proxy for your user wont be preserved when you execute sudo - to preserve it, do:

sudo -E yourcommand

I got my install working by first installing cntlm local proxy. The instructions here is succinct : http://www.leg.uct.ac.za/howtos/use-isa-proxies

Instead of student number, you'd put your domain username

2 To use the cntlm local proxy, exec:

pip install --proxy localhost:3128 pygments

Solution 3 - Python

You can install pip (or any other package) with easy_install almost as described in the first answer. However you will need a HTTPS proxy, too. The full sequence of commands is:

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

You might also want to add a port to the proxy, such as http{s}_proxy=http://proxy.myproxy.com:8080

Solution 4 - Python

You can try downloading the Windows binaries for pip from here: http://www.lfd.uci.edu/~gohlke/pythonlibs/#pip.

For using pip to download other modules, see @Ben Burn's answer.

Solution 5 - Python

For installing pip with get-pip.py behind a proxy I went with the steps below. My server was even behind a jump server.

From the jump server:

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

On the "python-server"

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

Success.

Solution 6 - Python

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

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionRolandoView Question on Stackoverflow
Solution 1 - PythonBen BurnsView Answer on Stackoverflow
Solution 2 - PythoneugenevdView Answer on Stackoverflow
Solution 3 - PythonAdrian WView Answer on Stackoverflow
Solution 4 - PythonDavid CView Answer on Stackoverflow
Solution 5 - PythonsastorslView Answer on Stackoverflow
Solution 6 - PythonGuru Raja YView Answer on Stackoverflow