Installing python modules through proxy

proxy, python, urllib2

Solution

Set the following environment variables:

HTTP_PROXY=http://user:password@your-company-proxy.com:8080

as well as

HTTPS_PROXY=http://user:password@your-company-proxy.com:8080

If your proxy port is not 8080, you should change 8080 with the appropriate port number too. If you don't have rights to modify the global system variables (you can only do so if you have local Admin rights), simply add it to your user-level variables.

Set it from `My Computer > Properties > Advanced > Environment Variables` (or "Advanced Properties" if in Windows 7)

Once you have that variable set, close all `cmd` windows and launch your command prompt again. Then you can use the normal setuptools `easy_install` and `pip` to download and install Python packages.

If you need to use it via Python; the `requests` library takes care of the quirks of `httplib` and `urllib`.

`requests` will automatically read `HTTP_PROXY` and use the proxy; but here is how you would do it manually (example from the docs):

import requests

proxies = {
  "http": "http://user:pass@foo.bar.zoo:8080",
  "https": "http://user:pass@foo.bar.zoo:8080",
}

requests.get("http://example.org", proxies=proxies)

Problem

I want to install a couple of python packages which use easy_install. They use the urrlib2 module in their setup script. I tried using the company proxy to let easy_install download the required packages. So to test the proxy conn I tried the following code. I dont need to supply any credentials for proxy in IE. ``` proxy = urllib2.ProxyHandler({"http":"http://mycompanyproxy-as-in-IE:8080"}) opener = urllib2.build_opener(proxy) urllib2.install_opener(opener) site = urllib2.urlopen("http://google.com") Error: Traceback (most recent call last): File "<stdin>", line 1, in <module> File "C:\Python27\lib\urllib2.py", line 126, in return _opener.open(url, data, timeout) File "C:\Python27\lib\urllib2.py", line 406, in response = meth(req, response) File "C:\Python27\lib\urllib2.py", line 519, in 'http', request, response, code, msg, hdrs) File "C:\Python27\lib\urllib2.py", line 444, in return self._call_chain(*args) File "C:\Python27\lib\urllib2.py", line 378, in result = func(*args) File "C:\Python27\lib\urllib2.py", line 527, in raise HTTPError(req.get_full_url(), code, msg urllib2.HTTPError: HTTP Error 407: AuthorizedOnly ``` Is it a problem with my code? or is the proxy not allowing a connection from the python process?. I can install R packages by setting the proxy.

Original source