通过代理安装python模块 [英] Installing python modules through proxy

查看:74
本文介绍了通过代理安装python模块的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想安装几个使用easy_install的python包.他们在安装脚本中使用 urrlib2 模块.我尝试使用公司代理让 easy_install 下载所需的包.所以为了测试代理连接,我尝试了以下代码.我不需要为 IE 中的代理提供任何凭据.

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

我的代码有问题吗?还是代理不允许来自 python 进程的连接?我可以通过设置代理来安装 R 包.

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.

推荐答案

设置以下环境变量:

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

以及

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

如果您的代理端口不是 8080,您也应该将 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.

我的电脑>设置它属性 >高级 >环境变量(或高级属性",如果在 Windows 7 中)

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

设置好该变量后,关闭所有 cmd 窗口并再次启动命令提示符.然后你就可以使用普通的安装工具easy_installpip来下载和安装Python包.

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.

如果需要通过Python使用;requests 库负责处理这些怪癖httpliburllib.

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

requests 会自动读取HTTP_PROXY 并使用代理;但这是您手动执行的方法(来自 docs):

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)

这篇关于通过代理安装python模块的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆