在VPN下,虚拟机中使用代理的Python pip install无法解析服务器

5 投票
1 回答
9509 浏览
提问于 2025-04-18 06:46

我的设置:

  • 在公司网络上的 Windows 7 64 位主机
  • Ubuntu 12.04 64 位的 VirtualBox 虚拟机
    • Ubuntu 虚拟机配置为使用 VirtualBox 桥接适配器
    • 代理设置确认可以使用:Firefox 可以访问公司内部网站和互联网,apt-get install 也能正常工作

到目前为止,我为配置代理做了以下工作:

/etc/profile.d/proxy.sh:

export http_proxy="http://<my_proxy>:<my_port>"
export https_proxy="http://<my_proxy>:<my_port>"
export ftp_proxy="http://<my_proxy>:<my_port>"

为了让 aptitude 工作,我修改了 /etc/apt/apt.conf.d/01proxy:

Acquire::http::Proxy “http://<my_proxy>:<my_port>"

为了在使用 sudo 时保留环境变量,我修改了 /etc/sudoers.d/proxy:

Defaults env_keep += "http_proxy https_proxy ftp_proxy"

我还在设置中设置了 HTTP/HTTPS/FTP 代理,路径是设置->网络->网络代理,并点击了“全局应用”。

问题是,当我尝试 pip install(无论是否加 sudo)时,都会出现错误:

$ pip -vv install git-review
Downloading/unpacking git-review
  Getting page http://pypi.python.org/simple/git-review
  Could not fetch URL http://pypi.python.org/simple/git-review: <urlopen error [Errno -2] Name or service not known>
  Will skip URL http://pypi.python.org/simple/git-review when looking for download links for git-review
  Getting page http://pypi.python.org/simple/
  Could not fetch URL http://pypi.python.org/simple/: <urlopen error [Errno -2] Name or service not known>
  Will skip URL http://pypi.python.org/simple/ when looking for download links for git-review
  Cannot fetch index base URL http://pypi.python.org/simple/
  URLs to search for versions for git-review:
  * http://pypi.python.org/simple/git-review/
  Getting page http://pypi.python.org/simple/git-review/
  Could not fetch URL http://pypi.python.org/simple/git-review/: <urlopen error [Errno -2] Name or service not known>
  Will skip URL http://pypi.python.org/simple/git-review/ when looking for download links for git-review
  Could not find any downloads that satisfy the requirement git-review
No distributions at all found for git-review
Exception information:
Traceback (most recent call last):
  File "/usr/lib/python2.7/dist-packages/pip/basecommand.py", line 126, in main
    self.run(options, args)
  File "/usr/lib/python2.7/dist-packages/pip/commands/install.py", line 223, in run
    requirement_set.prepare_files(finder, force_root_egg_info=self.bundle, bundle=self.bundle)
  File "/usr/lib/python2.7/dist-packages/pip/req.py", line 948, in prepare_files
    url = finder.find_requirement(req_to_install, upgrade=self.upgrade)
  File "/usr/lib/python2.7/dist-packages/pip/index.py", line 152, in find_requirement
    raise DistributionNotFound('No distributions at all found for %s' % req)
DistributionNotFound: No distributions at all found for git-review

我尝试使用 --proxy 选项(--proxy=http://<my_proxy>:<my_port>,有无等号和引号都试过),但即便如此,还是出现同样的错误。

我还尝试将 .pip/pip.conf 设置为如下:

[global]
timeout = 60
proxy = http://<my_proxy>:<my_port>

作为另一个参考,可能有帮助的是,从命令行使用 nslookup 查询公司内部网络以外的域名时失败,但查询内部网络的域名时可以成功。不过,正如前面提到的,Firefox 和 apt-get 在使用代理解析域名时似乎没有问题。

有什么想法吗?谢谢!

1 个回答

1

这是因为pip里面有个错误,这个问题在2014年6月就解决了。

https://github.com/pypa/pip/issues/1104

撰写回答