virtualenv 创建虚拟环境不成功

tkpromise
  • 90

在阿里云上面的服务器有一台服务器,我已经安装好pip和virtualenv,我通过

virtualenv venv

创建虚拟环境的时候提示的错误信息如下

root@Wireless:~/flasky# virtualenv venv
Running virtualenv with interpreter /usr/bin/python2
New python executable in /root/flasky/venv/bin/python2
Not overwriting existing python script /root/flasky/venv/bin/python (you must use /root/flasky/venv/bi                                                                                          n/python2)
Please make sure you remove any previous custom paths from your /root/.pydistutils.cfg file.
Installing setuptools, pkg_resources, pip, wheel...
  Complete output from command /root/flasky/venv/bin/python2 - setuptools pkg_resources pip wheel:
  Collecting setuptools
  Downloading http://mirrors.aliyun.com/pypi/packages/a7/62/3995dc3067cbf8d4a13ddac0f87417b9a08799c586                                                                                          b3faa77d62ad9085de/setuptools-34.1.0-py2.py3-none-any.whl (389kB)
Collecting pkg_resources
Exception:
Traceback (most recent call last):
  File "/usr/share/python-wheels/pip-8.1.1-py2.py3-none-any.whl/pip/basecommand.py", line 209, in main
    status = self.run(options, args)
  File "/usr/share/python-wheels/pip-8.1.1-py2.py3-none-any.whl/pip/commands/install.py", line 328, in                                                                                           run
    wb.build(autobuilding=True)
  File "/usr/share/python-wheels/pip-8.1.1-py2.py3-none-any.whl/pip/wheel.py", line 748, in build
    self.requirement_set.prepare_files(self.finder)
  File "/usr/share/python-wheels/pip-8.1.1-py2.py3-none-any.whl/pip/req/req_set.py", line 360, in prep                                                                                          are_files
    ignore_dependencies=self.ignore_dependencies))
  File "/usr/share/python-wheels/pip-8.1.1-py2.py3-none-any.whl/pip/req/req_set.py", line 512, in _pre                                                                                          pare_file
    finder, self.upgrade, require_hashes)
  File "/usr/share/python-wheels/pip-8.1.1-py2.py3-none-any.whl/pip/req/req_install.py", line 273, in                                                                                           populate_link
    self.link = finder.find_requirement(self, upgrade)
  File "/usr/share/python-wheels/pip-8.1.1-py2.py3-none-any.whl/pip/index.py", line 442, in find_requi                                                                                          rement
    all_candidates = self.find_all_candidates(req.name)
  File "/usr/share/python-wheels/pip-8.1.1-py2.py3-none-any.whl/pip/index.py", line 400, in find_all_c                                                                                          andidates
    for page in self._get_pages(url_locations, project_name):
  File "/usr/share/python-wheels/pip-8.1.1-py2.py3-none-any.whl/pip/index.py", line 545, in _get_pages
    page = self._get_page(location)
  File "/usr/share/python-wheels/pip-8.1.1-py2.py3-none-any.whl/pip/index.py", line 648, in _get_page
    return HTMLPage.get_page(link, session=self.session)
  File "/usr/share/python-wheels/pip-8.1.1-py2.py3-none-any.whl/pip/index.py", line 760, in get_page
    resp.raise_for_status()
  File "/root/flasky/venv/share/python-wheels/requests-2.9.1-py2.py3-none-any.whl/requests/models.py",                                                                                           line 840, in raise_for_status
    raise HTTPError(http_error_msg, response=self)
HTTPError: 404 Client Error: Not Found for url: http://mirrors.aliyun.com/pypi/simple/pkg-resources/
----------------------------------------
...Installing setuptools, pkg_resources, pip, wheel...done.
Traceback (most recent call last):
  File "/usr/lib/python3/dist-packages/virtualenv.py", line 2363, in <module>
    main()
  File "/usr/lib/python3/dist-packages/virtualenv.py", line 719, in main
    symlink=options.symlink)
  File "/usr/lib/python3/dist-packages/virtualenv.py", line 988, in create_environment
    download=download,
  File "/usr/lib/python3/dist-packages/virtualenv.py", line 918, in install_wheel
    call_subprocess(cmd, show_stdout=False, extra_env=env, stdin=SCRIPT)
  File "/usr/lib/python3/dist-packages/virtualenv.py", line 812, in call_subprocess
    % (cmd_desc, proc.returncode))
OSError: Command /root/flasky/venv/bin/python2 - setuptools pkg_resources pip wheel failed with error                                                                                           code 2

我发现我的系统里有3个版本的pip,如下

pip 9.0.1 from /usr/local/lib/python2.7/dist-packages (python 2.7)
root@Wireless:~/flasky# pip3 -V
pip 9.0.1 from /usr/local/lib/python3.5/dist-packages (python 3.5)
root@Wireless:~/flasky# pip2 -V
pip 8.1.1 from /usr/lib/python2.7/dist-packages (python 2.7)

我尝试过升级pip,但最后那个pip8.1无法升级到pip9,好像最高版本就是pip8,我
试过用以下命令进行升级:

pip install -U pip
pip install -U pip2
pip install -U pip3
pip2 install -U pip
pip2 install -U pip2
pip2 install -U pip3
pip3 install -U pip
pip3 install -U pip2
pip3 install -U pip3
回复
阅读 12.2k
5 个回答
Seven
  • 1
新手上路,请多包涵

HTTPError: 404 Client Error: Not Found for url: http://mirrors.aliyun.com/pyp...
这句说明阿里云服务器不向外发送请求。
解决方法:在服务器实例的“安全规则”---“外出方向”--“常用端口(TCP)” 选择http ;“授权策略”为允许。

nnnbbb
  • 2
新手上路,请多包涵

实际上是pkg-resoures中还有没有完全同步的资源.可以修改pip.conf为国外的源, 先备份, 更新后再备份过来. 以unbuntu 为例:

mv /root/.pip/pip.conf  /root/.pip/pip.conf.backup
vi /root/.pip/pip.conf 

修改如下:


[global]
# index-url=http://mirrors.aliyun.com/pypi/simple/  
index-url=https://pypi.python.org/simple/
[install]
# trusted-host=mirrors.aliyun.com  
trusted-host=pypi.python.org

然后在运行安装命令, 问题解决. 后面恢复的话

mv /root/.pip/pip.conf.backup /root/.pip/pip.conf 

https://blog.csdn.net/Tsinghu...

试试下面这个

virtualenv venv --python=python3.5

首先你得确认该使用哪个版本的 pip,如有必要可以这样尝试安装 pip3 install virtualenv,再执行上面的命令。

重启后解决,没有了

qinzibo
  • 2
新手上路,请多包涵

去/usr/share/python-wheels/下更换pip为9.0.1版本的whl文件是解决问题最好的办法

撰写回答
你尚未登录,登录后可以
  • 和开发者交流问题的细节
  • 关注并接收问题和回答的更新提醒
  • 参与内容的编辑和改进,让解决方法与时俱进
宣传栏