在代理服务器后面使用 ez_setup.py 安装 Python 的 easy_install
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/13733375/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Installing Python's easy_install using ez_setup.py from behind a proxy server
提问by jsears
Is there a way to install Python's easy_install using ez_setup.py when on a corporate network that uses a proxy server? Currently, I receive a connection timeout:
在使用代理服务器的公司网络上,有没有办法使用 ez_setup.py 安装 Python 的 easy_install?目前,我收到连接超时:
Downloading http://pypi.python.org/packages/2.7/s/setuptools/setuptools-0.6c11-py2.7.egg
Traceback (most recent call last):
File "C:\jsears\python\ez_setup.py", line 278, in <module>
main(sys.argv[1:])
File "C:\jsears\python\ez_setup.py", line 210, in main
egg = download_setuptools(version, delay=0)
File "C:\jsears\python\ez_setup.py", line 158, in download_setuptools
src = urllib2.urlopen(url)
File "C:\jsears\Python27\lib\urllib2.py", line 126, in urlopen
return _opener.open(url, data, timeout)
File "C:\jsears\Python27\lib\urllib2.py", line 400, in open
response = self._open(req, data)
File "C:\jsears\Python27\lib\urllib2.py", line 418, in _open
'_open', req)
File "C:\jsears\Python27\lib\urllib2.py", line 378, in _call_chain
result = func(*args)
File "C:\jsears\Python27\lib\urllib2.py", line 1207, in http_open
return self.do_open(httplib.HTTPConnection, req)
File "C:\jsears\Python27\lib\urllib2.py", line 1177, in do_open
raise URLError(err)
urllib2.URLError: <urlopen error [Errno 10060] A connection attempt failed because the connected party did not properly respond after a period of time, or established connection failed because connected host has failed to respond>
采纳答案by jsears
Apparently, you can simply set an environment variable:
显然,您可以简单地设置一个环境变量:
export http_proxy=http://<user>:<password>@<proxy_host_name>:<port>
export http_proxy=http://<user>:<password>@<proxy_host_name>:<port>
For example:
例如:
export http_proxy=http://admin:[email protected]:80
导出 http_proxy=http://admin:[email protected]:80
回答by namit
you can also set in your code:
您还可以在代码中设置:
import urllib2
proxy = urllib2.ProxyHandler({'http':'http://username:password@proxy_host:port'})
auth = urllib2.HTTPBasicAuthHandler()
opener = urllib2.build_opener(proxy, auth, urllib2.HTTPHandler)
urllib2.install_opener(opener)
回答by valir
On Windows 7, with PowerShell, the proxy settings above are ignored, and the tool won't work. But I found the solution.
在 Windows 7 上,使用 PowerShell,上面的代理设置将被忽略,该工具将无法工作。但我找到了解决方案。
I modified the routine download_file_powershell by adding
我通过添加修改了例程 download_file_powershell
[System.Net.WebRequest]::DefaultWebProxy.Credentials = [System.Net.CredentialCache]::DefaultCredentials;
inside the scriptlet used to download via the WebClient class. Here is the complete download_file_powershell function now:
在用于通过 WebClient 类下载的 scriptlet 中。现在是完整的 download_file_powershell 函数:
def download_file_powershell(url, target):
"""
Download the file at url to target using Powershell (which will validate
trust). Raise an exception if the command cannot complete.
"""
target = os.path.abspath(target)
cmd = [
'powershell',
'-Command',
"[System.Net.WebRequest]::DefaultWebProxy.Credentials = [System.Net.CredentialCache]::DefaultCredentials; (new-object System.Net.WebClient).DownloadFile(%(url)r, %(target)r)" % vars(),
]
subprocess.check_call(cmd)
回答by Andrew Martinez
If you already have a http_proxy/https_proxy environment variable set you can just tell ez_setup.py to not use PowerShell. PowerShell does not use HTTP_PROXY/HTTPS_PROXY environment variables. Follow the first section in this response.
如果你已经有一个 http_proxy/https_proxy 环境变量集,你可以告诉 ez_setup.py 不要使用 PowerShell。PowerShell 不使用 HTTP_PROXY/HTTPS_PROXY 环境变量。按照本回复中的第一部分进行操作。
For people who might not know how to set environment variables, see sections 2+.
对于可能不知道如何设置环境变量的人,请参阅第 2+ 节。
Stop ez_setup.py from using PowerShell
停止 ez_setup.py 使用 PowerShell
Go into the ez_install.py and find the following section:
进入 ez_install.py 并找到以下部分:
def has_powershell():
if platform.system() != 'Windows':
return False
cmd = ['powershell', '-Command', 'echo test']
devnull = open(os.path.devnull, 'wb')
try:
try:
subprocess.check_call(cmd, stdout=devnull, stderr=devnull)
except:
return False
finally:
devnull.close()
return True
and change it to be
并将其更改为
def has_powershell():
return False
ez_install.py will the use your environment HTTP_PROXY/HTTPS_PROXY which can be set from the command line or through the control panel.
ez_install.py 将使用您的环境 HTTP_PROXY/HTTPS_PROXY,可以从命令行或通过控制面板设置。
Temporary Command Line:
临时命令行:
set HTTP_PROXY=http://proxy.example.com
set HTTPS_PROXY=https://proxy.example.com
Note:if you do this you must run 'python ez_setup.py' in the same command window you ran these commands in.
注意:如果你这样做,你必须在你运行这些命令的同一个命令窗口中运行“python ez_setup.py”。
Permanent Command Line (User Only):
永久命令行(仅限用户):
setx HTTP_PROXY "http://proxy.example.com"
setx HTTPS_PROXY "https://proxy.example.com"
Permanent Command Line (Machine aka All Users):
永久命令行(机器又名所有用户):
setx HTTP_PROXY "http://proxy.example.com" /M
setx HTTPS_PROXY "https://proxy.example.com" /M
Permanent via Control Panel:
通过控制面板永久:
- Start -> Control Panel -> User Accounts
- On left panel, click "Change my environment variables"
- Click "New.." in "User variables" or "System Variables" (depending on what you want)
- Set Variable name: HTTP_PROXY and Variable value: http:/proxy.example.com
- Click "New.." in "User variables" or "System Variables" (depending on what you want)
- Set Variable name: HTTPS_PROXY and Variable value: https:/proxy.example.com
- Click 'Ok'
- 开始 -> 控制面板 -> 用户帐户
- 在左侧面板上,单击“更改我的环境变量”
- 单击“用户变量”或“系统变量”中的“新建..”(取决于您想要的)
- 设置变量名称:HTTP_PROXY 和变量值:http://proxy.example.com
- 单击“用户变量”或“系统变量”中的“新建..”(取决于您想要的)
- 设置变量名称:HTTPS_PROXY 和变量值:https:/proxy.example.com
- 点击“确定”
回答by LeeO
I just ran into the same problem and this is the solution I have found. I'll admit is not ideal, but it is only way I found around this issue on Windows.
我刚刚遇到了同样的问题,这是我找到的解决方案。我承认这并不理想,但这是我在 Windows 上找到解决此问题的唯一方法。
- Download. ez_setup.py.
- Edit the following line to show you where the file expect to download the zipped up package to:
- 下载。ez_setup.py。
- 编辑以下行以显示文件希望将压缩包下载到的位置:
print saveto
打印保存到
def download_setuptools(version=DEFAULT_VERSION, download_base=DEFAULT_URL,
to_dir=os.curdir, delay=15,
downloader_factory=get_best_downloader):
# making sure we use the absolute path
to_dir = os.path.abspath(to_dir)
tgz_name = "setuptools-%s.tar.gz" % version
url = download_base + tgz_name
saveto = os.path.join(to_dir, tgz_name)
print saveto
if not os.path.exists(saveto): # Avoid repeated downloads
log.warn("Downloading %s", url)
downloader = downloader_factory()
downloader(url, saveto)
return os.path.realpath(saveto)
This provides the following output when you execute the script in my case: "C:\Python27>python.exe ez_setup.py"
在我的情况下执行脚本时,这会提供以下输出:“C:\Python27>python.exe ez_setup.py”
Output:
输出:
C:\Python27\setuptools-1.4.2.tar.gz Downloading https://pypi.python.org/packages/source/s/setuptools/setuptools-1.4.2.tar.gzException calling "DownloadFile" with "2" argument(s): "The remote server returned an error: (407) Proxy Authentication Required." At line:1 char:47 + (new-object System.Net.WebClient).DownloadFile <<<< ('https://pypi.python.org/packages/source/s/setuptools/setuptools-1.4.2.tar.gz', 'C:\Python27\setuptools-1.4.2.tar.gz') + CategoryInfo : NotSpecified: (:) [], MethodInvocationException + FullyQualifiedErrorId : DotNetMethodException
C:\Python27\setuptools-1.4.2.tar.gz 下载https://pypi.python.org/packages/source/s/setuptools/setuptools-1.4.2.tar.gz使用“2”调用“DownloadFile”异常" 参数: "远程服务器返回错误:(407) 需要代理身份验证。" 在 line:1 char:47 + (new-object System.Net.WebClient).DownloadFile <<<< (' https://pypi.python.org/packages/source/s/setuptools/setuptools-1.4.2. tar.gz', 'C:\Python27\setuptools-1.4.2.tar.gz') + CategoryInfo : NotSpecified: (:) [], MethodInvocationException +fullyQualifiedErrorId : DotNetMethodException
- Download the pack from the https link above and put it in location it expects in my case "C:\Python27\"
- 从上面的 https 链接下载包并将其放在我的情况下期望的位置“C:\Python27\”
This file being placed in the location triggers this logic statement:
放置在该位置的文件会触发以下逻辑语句:
if not os.path.exists(saveto): # Avoid repeated downloads
log.warn("Downloading %s", url)
downloader = downloader_factory()
downloader(url, saveto)
return os.path.realpath(saveto)
As if by magic, the package will be installed.
就像魔法一样,将安装该软件包。

