Ubuntu – Cannot install python venv on Ubuntu 20.04 after upgrading from Bionic

python3

After a system update to Ubuntu 20.04, I've had various issues with python. The current problem is that I cannot create a virtual environment:

python3 -m venv env

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: ['/home/XXX/my-project/env/bin/python3', '-Im', 'ensurepip', '--upgrade', '--default-pip']

To find out more about why it is failing I ran
./env/bin/python3 -Im ensurepip --upgrade --default-pip:

/env/bin/python3: Error while finding module specification for 'ensurepip.__main__' (ImportError: cannot import name '_bundled' from partially initialized module 'ensurepip' (most likely due to a circular import) (/usr/lib/python3.9/ensurepip/__init__.py))

Of course I tried the suggested step with sudo apt-get install python3-venv:

Reading package lists... Done
Building dependency tree       
Reading state information... Done
Some packages could not be installed. This may mean that you have
requested an impossible situation or if you are using the unstable
distribution that some required packages have not yet been created
or been moved out of Incoming.
The following information may help to resolve the situation:

The following packages have unmet dependencies:
 python3-venv : Depends: python3.8-venv (>= 3.8.2-1~) but it is not going to be installed
E: Unable to correct problems, you have held broken packages.

lsb_release -a output:

Description: Ubuntu 20.04.1

LTS Release: 20.04

Codename: focal

whereis python output:

python: /usr/bin/python3.6 /usr/bin/python3.7 /usr/bin/python2.7-config /usr/bin/python2.7 /usr/bin/python3.6m /usr/bin/python3.9 /usr/bin/python3.8 /usr/bin/python /usr/bin/python3.7m /usr/lib/python3.6 /usr/lib/python3.7 /usr/lib/python2.7 /usr/lib/python3.9 /usr/lib/python3.8 /etc/python3.6 /etc/python3.7 /etc/python2.7 /etc/python3.9 /etc/python3.8 /etc/python /usr/local/lib/python3.6 /usr/local/lib/python3.7 /usr/local/lib/python2.7 /usr/local/lib/python3.9 /usr/local/lib/python3.8 /usr/include/python3.6 /usr/include/python2.7 /usr/include/python3.6m /usr/include/python3.8 /usr/share/python

/etc/apt/sources.list only contains focal software sources.

also ran

sudo apt update  
sudo apt upgrade   

removed python3.9, sudo apt install python3.8-venv fails with the same error

python3.8-venv : Depends: python3.8 (= 3.8.5-1~20.04) but 3.8.6-1+bionic1 is to be installed
E: Unable to correct problems, you have held broken packages.

dpkg --get-selections | grep hold returns empty

Best Answer

Looking at the info you provided, your problem might be due to the python3.9 installation.

In Ubuntu 20.04, the default python version is 3.8. So other packages that depend on python should be expecting python3.8.

However, in your system, this default was changed to Python 3.9 as the attempt to create a new venv ended up with a Python 3.9 environment as observed in the outputs.

As your original error mentions, you need to install the python3-venv package using apt. But since python3.9 was installed as well, I believe apt isn't able to figure out anymore what to install. So explicitly mentioning the full version may solve your problem.

sudo apt install python3.8-venv

ref: apt-get, unmet dependencies, ... "but it is not going to be installed"

After that is done, you need to create a venv by also explicitly passing the full python version

python3.8 -m venv env

If your intention is to use Python 3.9, and as long as it was installed with apt, you can fix it by installing python3.9-venv instead of python3.8-venv

sudo apt install python3.9-venv

ps. You will need to create a new virtual environment again after installing the apt package. The previous one will stay broken.

Related Question