简体   繁体   English

如何在 Ubuntu 18.04 上安装 python3.7 并使用 pip 创建一个 virtualenv?

[英]How to install python3.7 and create a virtualenv with pip on Ubuntu 18.04?

I'm trying to set up a standard virtual-environment(venv) with python 3.7 on Ubuntu 18.04, with pip (or some way to install packages in the venv).我正在尝试在 Ubuntu 18.04 上使用 python 3.7 设置标准虚拟环境(venv),使用 pip(或某种在 venv 中安装软件包的方式)。 The standard way to install python3.7 seems to be:安装python3.7的标准方法似乎是:

% sudo apt install python3.7 python3.7-venv
% python3.7 -m venv py37-venv

but the second command fails, saying:但第二个命令失败,说:

The virtual environment was not created successfully because ensurepip is not available.由于 ensurepip 不可用,未成功创建虚拟环境。 On Debian/Ubuntu systems, you need to install the python3-venv package using the following command.在 Debian/Ubuntu 系统上,您需要使用以下命令安装 python3-venv 包。

 apt-get install python3-venv

You may need to use sudo with that command.您可能需要在该命令中使用 sudo。 After installing the python3-venv package, recreate your virtual environment.安装 python3-venv 包后,重新创建您的虚拟环境。

Failing command: ['/py37-venv/bin/python3.7', '-Im', 'ensurepip', '--upgrade', '--default-pip']失败的命令:['/py37-venv/bin/python3.7', '-Im', 'ensurepip', '--upgrade', '--default-pip']

This is true;这是真实的; there is no ensurepip nor pip installed with this python.此 python 没有安装 ensurepip 或 pip。 And I did install python3.7-venv already ( python3-venv is for python3.6 on Debian/Ubuntu).而且我确实已经安装python3.7-venvpython3-venv用于 Debian/Ubuntu 上的 python3.6)。 I gather there has been some discussion about this in the python community because of multiple python versions and/or requiring root access, and alternate ways to install python modules via apt or similar.我知道在 python 社区中已经对此进行了一些讨论,因为有多个 python 版本和/或需要 root 访问权限,以及通过apt或类似方式安装 python 模块的替代方法。

Creating a venv without pip ( --without-pip ) succeeds, but then there's no way to install packages in the new venv which seems to largely defeat the purpose.创建没有 pip ( --without-pip ) 的 venv 成功,但是无法在新的 venv 中安装软件包,这似乎在很大程度上违背了目的。

So what's the accepted "best practice" way to install and use python3.7 on 18.04 with a venv?那么在 18.04 上使用 venv 安装和使用 python3.7 的公认“最佳实践”方式是什么?

I don't know if it's best practices or not, but if I also install python3-venv and python3.7-venv then everything works (this is tested on a fresh stock Debian buster docker image):我不知道这是否是最佳实践,但如果我还安装了 python3-venv 和 python3.7-venv 则一切正常(这是在新鲜的 Debian buster docker 映像上测试的):

% sudo apt install python3.7 python3-venv python3.7-venv
% python3.7 -m venv py37-venv
% . py37-venv/bin/activate
(py37-venv) % 

Note that it also installs all of python3.6 needlessly, so I can't exactly say I like it, but at least it does work and doesn't require running an unsigned script the way get-pip.py does.请注意,它还不必要地安装了所有 python3.6,所以我不能完全说我喜欢它,但至少它确实有效并且不需要像get-pip.py那样运行未签名的脚本。

sudo apt install python3-venv
python3 -m venv env

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM