12

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

8
  • Does this answer your question? Unable to correct problems, you have held broken packages
    – karel
    Oct 21, 2020 at 13:16
  • dpkg --get-selections doesn't find any held packages but I'll try the other suggestions
    – kosmičák
    Oct 21, 2020 at 13:24
  • 1
    You seem to have python3.9 installed which is not installed by default on 20.04. The python-venv package may be having issues getting installed because of that. (just speculating here)
    – Dan
    Oct 21, 2020 at 13:26
  • 2
    Does this help with the dependency problem? askubuntu.com/a/606896 Even if it does, I still recommend making sure that the python3.9 installation didn't mess up your system. You have changed the default environment for Ubuntu to use python3.9 by default. It may cause similar issues later with other packages if that was the case.
    – Dan
    Oct 21, 2020 at 13:37
  • 1
    AskUbuntu is not a threaded help forum that can wind from topic to topic (ubuntuforums.org is). AskUbuntu is a Question/Answer site. This is your third independent question trying to squeeze into a single question, which makes it hard for volunteers here to answer usefully. Remember that questions and answers should help future users, too.
    – user535733
    Oct 21, 2020 at 14:04

5 Answers 5

10

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.

3
  • 2
    sudo apt install python3.8-venv throws the same error: The following packages have unmet dependencies: 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
    – kosmičák
    Oct 21, 2020 at 14:04
  • 2
    @kosmičák Try running sudo apt update then run the install command.
    – Dan
    Oct 21, 2020 at 14:23
  • You have an extra PPA that is trying to install a newer version of python3.8. You will need to disable that as it's causing this conflict
    – Dan
    Oct 21, 2020 at 14:34
1

I had a simmilar problem. I can't install python3.8-venv package, because of a newer version of python3.8 package (with ~20.04 postfix). It turned out that I hadn't focal-security source in my source.list.

Adding deb http://security.ubuntu.com/ubuntu focal-security main universe in /etc/apt/sources.list fixed the problem.

1
  • After adding deb http://security.ubuntu.com/ubuntu focal-security main universe in /etc/apt/sources.list, we need to run apt update and then apt upgrade python-pip-whl python3-pip. It works for me now.
    – mathsyouth
    Aug 17, 2022 at 3:11
1

Using aptitude instead of apt-get fixed it for me.

  1. Install aptitude sudo apt-get install aptitude
  2. Now try installing python3.8-venv with aptitude and follow the steps as shown in the below screenshot enter image description here

Once the above steps were completed, python3 -m venv env command worked fine.

0

I was facing the same problem in Ubuntu 20.04 which was not going away event after apt update command. I updated the apt repo url in /etc/apt/sources.list from http://in.archive.ubuntu.com/ubuntu/ to http://us.archive.ubuntu.com/ubuntu/. Which resolved the problem for me.

-1

Turns out I had to keep removing various bionic python packages:

sudo apt remove python3.8
sudo apt remove python3.8-minimal
sudo apt remove libpython3.8-stdlib
sudo apt remove libpython3.8-minimal

Whenever it complained about them...

The following packages have unmet dependencies:
 python3.8 : Depends: python3.8-minimal (= 3.8.5-1~20.04) but 3.8.6-1+bionic1 is to be installed
             Depends: libpython3.8-stdlib (= 3.8.5-1~20.04) but 3.8.6-1+bionic1 is to be installed

... I removed them

...until I could (re)install these:

sudo apt install python3.8-minimal
sudo apt install python3.8
sudo apt install python3.8-venv
sudo apt install python3-distutils

and I could finally run python3.8 -m venv env

So it seems that the upgrade from Bionic Beaver to Focal Fossa partially left old python packages behind which introduced the incompatibilites.

2
  • 6
    -1. removing 3.8 can uninstall ubuntu-desktop, not recommended, be careful doing this May 11, 2021 at 8:31
  • true, that's what actually happened in my case :) I couldn't boot into X after that, had to reinstall the missing packages from terminal and afterwards everything was working again.
    – kosmičák
    May 12, 2021 at 9:40

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .