Fixing the Python venv ensurepip Issue

python venv wsl

Posted: 2023-12-24

Having recently switched back to Windows 10 from being an exclusive Manjaro Linux user for the past 3 years reintroduced me to working with WSL (Windows Subsystem for Linux) and Ubuntu 20.

With a fresh install of Ubuntu 20, I noticed I was unable to create python virtual environments using the regular command python -m venv .venv. It was giving me an error message saying that ensurepip failed.

Error: Command '['path/to/.venv/bin/python', '-Im', 'ensurepip', '--upgrade', '--default-pip']' returned non-zero exit status 1.

I googled frantically and found numerous articles saying that I have to install the two packages python3.x-venv and python3-pip using sudo apt install and so I did. This did however not fix my issue.

Trying the ensurepip command outright (path/to/.venv/bin/python -m ensurepip) gave me a little more insight into why it was failing. It seems to be something with the lsb_release command not working.

subprocess.CalledProcessError: Command '('lsb_release', '-a')' returned non-zero exit status 1.

After some more googling I stumbled onto this article on Stackoverflow which point to the issue being that the ‘lsb_release’ python module has been removed from the python distribution. Creating a symlink in the python distribution packages directory to the modules common location solved the issue.

For me however, the symlink command had to be modified to sudo ln -s /usr/share/pyshared/lsb_release.py /usr/lib/python3/dist-packages/lsb_release.py due to the -I flag used in the ensurepip command above.

Hope this cures your frustration like it did mine!

Cheers,
Theo