使用ez_setup.py从代理服务器后面安装Python的easy_install

jse*_*ars 12 python proxy easy-install

有没有办法在使用代理服务器的企业网络上使用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>
Run Code Online (Sandbox Code Playgroud)

小智 15

在Windows 7上,使用PowerShell,上面的代理设置将被忽略,该工具将无法工作.但我找到了解决方案.

我通过添加修改了例程download_file_powershell

[System.Net.WebRequest]::DefaultWebProxy.Credentials = [System.Net.CredentialCache]::DefaultCredentials;
Run Code Online (Sandbox Code Playgroud)

用于通过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)
Run Code Online (Sandbox Code Playgroud)


And*_*nez 7

如果您已经设置了http_proxy/https_proxy环境变量,则可以告诉ez_setup.py不要使用PowerShell.PowerShell不使用HTTP_PROXY/HTTPS_PROXY环境变量.请遵循此回复中的第一部分.

对于可能不知道如何设置环境变量的人,请参阅第2节以上的内容.

停止使用PowerShell ez_setup.py

进入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
Run Code Online (Sandbox Code Playgroud)

并改变它

def has_powershell():
     return False
Run Code Online (Sandbox Code Playgroud)

ez_install.py将使用您的环境HTTP_PROXY/HTTPS_PROXY,可以从命令行或通过控制面板进行设置.

临时命令行:

set HTTP_PROXY=http://proxy.example.com
set HTTPS_PROXY=https://proxy.example.com
Run Code Online (Sandbox Code Playgroud)

注意:如果这样做,您必须在运行这些命令的同一命令窗口中运行'python ez_setup.py'.

永久命令行(仅限用户):

setx HTTP_PROXY "http://proxy.example.com"
setx HTTPS_PROXY "https://proxy.example.com"
Run Code Online (Sandbox Code Playgroud)

永久命令行(Machine aka All Users):

setx HTTP_PROXY "http://proxy.example.com" /M
setx HTTPS_PROXY "https://proxy.example.com" /M
Run Code Online (Sandbox Code Playgroud)

永久通过控制面板:

  1. 开始 - >控制面板 - >用户帐户
  2. 在左侧面板中,单击"更改我的环境变量"
  3. 单击"用户变量"或"系统变量"中的"新建..."(取决于您的需要)
  4. 设置变量名称:HTTP_PROXY和变量值:http:/proxy.example.com
  5. 单击"用户变量"或"系统变量"中的"新建..."(取决于您的需要)
  6. 设置变量名称:HTTPS_PROXY和变量值:https:/proxy.example.com
  7. 点击"确定"


jse*_*ars 6

显然,您可以简单地设置一个环境变量:

export http_proxy = http:// <user>:<password> @ <proxy_host_name>:<port>

例如:

export http_proxy = http:// admin:password@proxy.example.com:80

  • 在Windows上,这两个环境变量似乎无法正常工作 (4认同)