如何在 linux ubuntu 18.04 python 3.7 上使用 pip 安装 venv

roy*_*233 6 python pip virtualenv

我试图安装venv使用pip,它给了我下面的错误信息:

这是命令:

$ pip install venv

Run Code Online (Sandbox Code Playgroud)

和错误:

Collecting venv
Exception:
Traceback (most recent call last):
  File "/usr/lib/python2.7/dist-packages/pip/basecommand.py", line 215, in main
    status = self.run(options, args)
  File "/usr/lib/python2.7/dist-packages/pip/commands/install.py", line 353, in run
    wb.build(autobuilding=True)
  File "/usr/lib/python2.7/dist-packages/pip/wheel.py", line 749, in build
    self.requirement_set.prepare_files(self.finder)
  File "/usr/lib/python2.7/dist-packages/pip/req/req_set.py", line 380, in prepare_files
    ignore_dependencies=self.ignore_dependencies))
  File "/usr/lib/python2.7/dist-packages/pip/req/req_set.py", line 554, in _prepare_file
    require_hashes
  File "/usr/lib/python2.7/dist-packages/pip/req/req_install.py", line 278, in populate_link
    self.link = finder.find_requirement(self, upgrade)
  File "/usr/lib/python2.7/dist-packages/pip/index.py", line 465, in find_requirement
    all_candidates = self.find_all_candidates(req.name)
  File "/usr/lib/python2.7/dist-packages/pip/index.py", line 423, in find_all_candidates
    for page in self._get_pages(url_locations, project_name):
  File "/usr/lib/python2.7/dist-packages/pip/index.py", line 568, in _get_pages
    page = self._get_page(location)
  File "/usr/lib/python2.7/dist-packages/pip/index.py", line 683, in _get_page
    return HTMLPage.get_page(link, session=self.session)
  File "/usr/lib/python2.7/dist-packages/pip/index.py", line 795, in get_page
    resp.raise_for_status()
  File "/usr/share/python-wheels/requests-2.18.4-py2.py3-none-any.whl/requests/models.py", line 935, in raise_for_status
    raise HTTPError(http_error_msg, response=self)
HTTPError: 404 Client Error: Not Found for url: https://pypi.org/simple/venv/
Run Code Online (Sandbox Code Playgroud)

操作系统:linux ubuntu 18.04 python 3.7

谢谢!

RMP*_*MPR 10

在 Ubuntu 18.04 上,python可执行文件默认绑定python2.7现在EOL);所以当你这样做时:

pip install venv
Run Code Online (Sandbox Code Playgroud)

您正在尝试安装venv在仅与 python3.3+ 兼容的 Python2.7(见下文)上,这就是您收到这些错误的原因。

附带说明一下,您不需要venvpython3.3+上安装,因为它默认包含在内,请参阅venv 文档,但分发维护者可能会在没有此模块的情况下提供 Python,因此您必须使用包管理器安装它,在此案件:

sudo apt install python3-venv -y 
Run Code Online (Sandbox Code Playgroud)

通过执行命令来创建虚拟环境:

sudo apt install python3-venv -y 
Run Code Online (Sandbox Code Playgroud)

对于以前版本的 Python(在您的情况下为 2.7),您可以使用 virtualenv

python3 -m venv /path/to/new/virtual/environment
Run Code Online (Sandbox Code Playgroud)

和创作:

pip install virtualenv
Run Code Online (Sandbox Code Playgroud)

请注意,该-p标志不是强制性的,它只是为了确保您调用了正确的 Python 版本,以防您同时安装了 Python 2 和 Python 3 的 virtualenv。

  • 不要将 python3 符号链接到 python。在 Ubuntu 上它会破坏系统。 (2认同)