38

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). 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:

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']

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.

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.

So what's the accepted "best practice" way to install and use python3.7 on 18.04 with a venv?

ankostis
  • 8,579
  • 3
  • 47
  • 61
GaryO
  • 5,873
  • 1
  • 36
  • 61
  • Well, did you install Python 3.7 or not? If yes, fix your topic line. Then, what is the full output of the command you run? Preferably don't abbreviate but quote verbatim. That makes it searchable (which you have done before asking, right?) Also, Python itself doesn't require venv support, so maybe you need to install some more things... – Ulrich Eckhardt Oct 30 '18 at 18:44
  • 1
    Python3.7 is installed successfully by the commands above, however neither 'pip' nor any virtualenv system is installed. So the "and" part of my question is not satisfied; I think the question text stands. I'll update the question with the full output of the commands if you think that is useful. – GaryO Oct 30 '18 at 19:18
  • 1
    To get pip: `curl -O https://bootstrap.pypa.io/get-pip.py` and to install pip: `sudo python get-pip.py` – racket99 Oct 30 '18 at 19:26
  • 1
    To install virtualenv: `pip install virtualenv` and to use: `virtualenv --python= /path/to/venv` and `source venv/bin/activate`. You should see the name of your virtual environment as (venv) in your prompt. – racket99 Oct 30 '18 at 19:30
  • The above commands only work on Debian/Ubuntu after doing `apt install python3.7-distutils` in addition to installing python. But then they do work. – GaryO Oct 30 '18 at 19:35
  • 4
    Although this is not an answer - it looks to me that `python3.7-venv` is mostly a copy of `python3.6-venv`, including the system `pip` check; however, the package `python3.7-pip` doesn't exist for ubuntu bionic, leaving python 3.7 without a system `pip`. Overall, this looks like an ubuntu issue to me and should be fixed. – hoefling Oct 30 '18 at 20:32
  • Not only Ubuntu issue. Similar behavior on vanilla Debian 9. – Tuukka Mustonen Mar 21 '19 at 07:26

2 Answers2

79

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):

% 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.

Community
  • 1
  • 1
GaryO
  • 5,873
  • 1
  • 36
  • 61
  • This answer does not address the original issue: Q asks for Ubuntu("bionic")-18.07 and the answer talks about another distribution, Debian("buster")-10.0. Problems related to `python3venv` are highly sensitive to: a) to the specific Debian/Ubuntu release we're talking about, b) whether the asked `venv` to install is the default for that distribution-release or an older/later one, and c) any customized apt-repos in `/etc/apt/sources.list`. – ankostis Jan 22 '21 at 11:09
  • How and where to install python3.7, python3-venv and python3.7-venv packages that apy installs for us manually? Reason I am asking is because I cannot use yum with internet connection there need to download it from somewhere then be transferred. – haneulkim Jan 19 '23 at 04:52
-8
sudo apt install python3-venv
python3 -m venv env
geisterfurz007
  • 5,292
  • 5
  • 33
  • 54
  • 5
    Please put your answer always in context instead of just pasting code. See [here](https://stackoverflow.com/help/how-to-answer) for more details. – gehbiszumeis Jan 14 '20 at 06:38
  • This is a wrong solution, you set up a virtualenv with default python, and on Ubuntu 18.04 default python is python3.6 not python3.7 . – Yuri Baburov Jun 15 '20 at 10:20
  • not only is it wrong, it's basically the same as the correct answer which came many months earlier, with the useful bits removed. – Phil Jul 21 '20 at 10:53