110

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_proxy environment variable. But when I try to install a package like this:

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.

Annihilator8080
  • 2,019
  • 3
  • 21
  • 20
  • 3
    can you browse to pypi.python.org in your browser? – Shani Sep 29 '13 at 19:19
  • Yes. I can browse the internet without a problem. I have also configured proxy for wget in the wgetrc file and `wget` seems to be working. Even `git` is working with some configuring of proxy. But `pip` keeps throwing the same error. – Annihilator8080 Sep 30 '13 at 04:38
  • See this answer to a similar question. http://stackoverflow.com/questions/11726881/how-to-set-an-http-proxy-in-python-2-7 – mordechai Jul 05 '15 at 10:09

6 Answers6

149

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

You should use the following:

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

Also, the HTTP_PROXY env var should be respected.

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

svvac
  • 5,814
  • 3
  • 17
  • 22
  • 4
    Where's the difference? – svvac May 15 '14 at 16:39
  • 9
    Actually you have to specify (protocol is required): `pip install --proxy http://user:password@proxyserver:port ` – Stanislav May 22 '14 at 09:53
  • 1
    In my case I have to specify the domain, like this: pip install --proxy DOMAIN\user:password@proxyserver:port – cag May 27 '16 at 07:27
  • 1
    I suppose you're connecting to a proxy authenticated by some kind of windows ActiveDirectory service. The domain is thus somehow part of the username. Be careful, you may need to escape `\` to avoid terminal char escaping (e.g. `\n` and the likes) – svvac May 27 '16 at 07:51
  • Obviously I meant escaping `\\`, but I can't edit now ;-) – svvac May 27 '16 at 08:07
  • most of the repos are have the URL started with https:// So better you also suggest to try https:// with proxy URL – John Chornelius Sep 24 '18 at 09:17
  • Is the env var "HTTP_PROXY" or "http_proxy" (lowercase)? I set both in my env but wonder if both are necessary. Most of the articles and comments use "http_proxy" (lowercase). – Lawrence I. Siden Mar 20 '20 at 13:32
  • In my case, I have to remove the *scheme ://* part for http proxy. I am using pip 21.2.4. – smwikipedia Sep 29 '21 at 10:04
36

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.

export https_proxy="http://<proxy.server>:<port>"
pip install TwitterApi
petre
  • 1,485
  • 14
  • 24
22

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

Example:

$ sudo pip install --proxy=http://yourproxy:yourport package_name
Tudor Leustean
  • 517
  • 3
  • 15
17

First Try to set proxy using the following command

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

Then Try using the command

pip install ModuleName
Tom
  • 4,257
  • 6
  • 33
  • 49
Karthik C
  • 201
  • 2
  • 4
  • 2
    This worked for me when none of the other solutions did. Using Windows 7 Pro – Plamen Mar 30 '17 at 14:04
  • 1
    @Plamen, thats because most of the pip repos start with https:// All other answers suggests to use http_proxy, but in this case, you have set https_proxy – John Chornelius Sep 24 '18 at 09:20
9

On Ubuntu, you can set proxy by using

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

or if you are having SOCKS error use

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

Then run pip

sudo -E pip3 install {packageName}
Gary Mendonca
  • 1,925
  • 1
  • 13
  • 21
6

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

Final command, which worked for me:

sudo pip3 install --proxy https://{proxy}:{port} {BINARY}
René Vogt
  • 43,056
  • 14
  • 77
  • 99
Ruben
  • 61
  • 1
  • 1