Installing python modules through proxy

那年仲夏 提交于 2019-12-01 08:09:54
Burhan Khalid

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)

You can execute following command:

sudo pip --proxy < proxy > install < module >

via Windows cmd/PowerShell using setx http_proxy and https_proxy worked.

Both were needed as simply setting http_proxy was not sufficient.

As mentioned above but configured for Windows:

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

as well as

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

The error says that you need to authorization also. Try following code:

proxy = urllib2.ProxyHandler({"http":"http://mycompanyproxy-as-in-IE:8080"})
proxy_auth_handler = urllib2.HTTPBasicAuthHandler()
proxy_auth_handler.add_password('realm', 'host', 'username', 'password')
opener = build_opener(proxy, proxy_auth_handler)
urllib2.install_opener(opener)
site = urllib2.urlopen("http://google.com")

I think this should work.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!