如何在 Ubuntu 18.04 上安装 python3.7 并使用 pip 创建一个 virtualenv?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/53070868/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to install python3.7 and create a virtualenv with pip on Ubuntu 18.04?
提问by GaryO
I'm trying to set up a standard virtualenv with python 3.7 on Ubuntu 18.04, with pip (or some way to install packages in the virtualenv). The standard way to install python3.7 seems to be:
% sudo apt install python3.7 python3.7-venv
% python3.7 -m venv py37-venv
but the second command fails, saying:
我正在尝试在 Ubuntu 18.04 上使用 python 3.7 设置标准 virtualenv,使用 pip(或在 virtualenv 中安装软件包的某种方式)。安装python3.7的标准方法似乎是:
% sudo apt install python3.7 python3.7-venv
% python3.7 -m venv py37-venv
但第二个命令失败,说:
The virtual environment was not created successfully because ensurepip is not available. On Debian/Ubuntu systems, you need to install the python3-venv package using the following command.
apt-get install python3-venv
You may need to use sudo with that command. After installing the python3-venv package, recreate your virtual environment.
Failing command: ['/py37-venv/bin/python3.7', '-Im', 'ensurepip', '--upgrade', '--default-pip']
虚拟环境未成功创建,因为 ensurepip 不可用。在 Debian/Ubuntu 系统上,您需要使用以下命令安装 python3-venv 包。
apt-get install python3-venv
您可能需要在该命令中使用 sudo。安装 python3-venv 包后,重新创建您的虚拟环境。
失败的命令:['/py37-venv/bin/python3.7'、'-Im'、'ensurepip'、'--upgrade'、'--default-pip']
This is true; there is no ensurepip nor pip installed with this python. And I did install python3.7-venv
already (python3-venv
is for python3.6 on Debian/Ubuntu). 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没有安装ensurepip或pip。我已经安装python3.7-venv
了(python3-venv
适用于 Debian/Ubuntu 上的 python3.6)。我收集到在 python 社区中有一些关于这个的讨论,因为有多个 python 版本和/或需要 root 访问权限,以及通过apt
或类似方式安装 python 模块的替代方法。
Creating a virtualenv without pip (--without-pip
) succeeds, but then there's no way to install packages in the new virtualenv which seems to largely defeat the purpose.
在没有 pip ( --without-pip
) 的情况下创建 virtualenv成功了,但是没有办法在新的 virtualenv 中安装软件包,这似乎在很大程度上违背了目的。
So what's the accepted "best practice" way to install and use python3.7 on 18.04 with a virtualenv?
那么在 18.04 上使用 virtualenv 安装和使用 python3.7 的公认“最佳实践”方式是什么?
回答by GaryO
I don't know if it's best practices or not, but if I also install python3-venv then everything works (this is tested on a fresh stock Debian buster docker image):
我不知道这是否是最佳实践,但如果我还安装了 python3-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
。
回答by snehashish debnath
sudo apt install python3-venv
python3 -m venv env