pyvenv returns non-zero exit status 1 (during the installation of pip stage)

16.5k views Asked by At

If you should ever encounter the following error when creating a Python virtual environment using the pyvenv command:

user$ pyvenv my_venv_dir
Error: Command '['/home/user/my_venv_dir/bin/python', '-Im', 'ensurepip', '--upgrade', '--default-pip']' returned non-zero exit status 1

... then the answer (below) provides a simple way to work around it, without resorting to setuptools and it's related acrobatics.

4

There are 4 answers

1
NYCeyes On BEST ANSWER

Here's an approach that is fairly O/S agnostic...

Both the pyvenv and python commands themselves include a --without-pip option that enable you to work around this issue; without resorting to setuptool or other headaches. Taking note of my inline comments below, here's how to do it, and is very easy to understand:

user$ pyvenv --without-pip ./pyvenv.d          # Create virtual environment this way;
user$ python -m venv --without-pip ./pyvenv.d  # --OR-- this newer way. Both work.

user$ source ./pyvenv.d/bin/activate  # Now activate this new virtual environment.
(pyvenv.d) user$

# Within it, invoke this well-known script to manually install pip(1) into /pyvenv.d:
(pyvenv.d) user$ curl https://bootstrap.pypa.io/get-pip.py | python

(pyvenv.d) user$ deactivate           # Next, reactivate this virtual environment,
user$ source ./pyvenv.d/bin/activate  # which will now include the pip(1) command.
(pyvenv.d) user$

(pyvenv.d) user$ which pip            # Verify that pip(1) is indeed present.
/path/to/pyvenv.d/bin/pip

(pyvenv.d) user$ pip install --upgrade pip # And finally, upgrade pip(1) itself;
(pyvenv.d) user$                           # although it will likely be the
                                           # latest version already.
# And that's it!

I hope this helps. \(◠﹏◠)/

0
Niloct On

If you have a module whose file is named after a python standard library, in the folder on which you invoke python -m venv venv, then this command will fail without a hint about that.

For instance, you name a file email.py.

What I did to find this was coding a bash script which moves the .py files off the current directory one by one (to a holdspace/ subdir), and try at each move to invoke the venv dir creation. If the python -m venv venv command exits with 0 code, so it's successful and the last file moved was the culprit.


#!/bin/bash
if [ ! -d ./holdspace ]
then
  mkdir holdspace/
fi

for file in *.py
do
  mv "$file" holdspace/
  python -m venv venv >/dev/null 2>&1
  if [ $? -eq 0 ]
  then
    echo "$file was the culprit."
    rm -rf venv/
    break
  else
    echo "$file isn't the culprit."
  fi
  rm -rf venv/
done

mv holdspace/*.py .
0
Arunabh Das On

The following should fix it

brew update
brew upgrade
brew install zlib
1
tjfuller On

2020, on python3.8 on WSL2 (Ubuntu) the following solved this for me:

sudo apt install python3.8-venv