'Error when creating venv, Error: Command '-Im', 'ensurepip', '--upgrade', '--default-pip']' returned non-zero exit status 1

When I try to create venv it throws this error:

Error: Command '['C:\\Users\\admin\\env\\Scripts\\python.exe', '-Im', 'ensurepip', '--upgrade', '--default-pip']' returned non-zero exit status 1.

It is strange for me because I'm using python for a long time and never had such a problem.



Solution 1:[1]

On Ubuntu first, install the venv of the specific python version sudo apt install python3.9-venv

python3.9 -m venv myenv //specify the python version. This will create the virtual env "myenv"

Solution 2:[2]

I am using windows 10 WSL2 ubuntu 20.04, sudo resolved my problem.

sudo python3.8 -m venv venv

Solution 3:[3]

1- head over this doc and try to refix your global python installation accordingly, don't forget to check Install launcher for all users option, after successful installation the py launcher will be localed under C:\Windows folder.

2- use isolated vrtual environement, venv built-in module is recommended over other 3rd tools and just avoid to mess with your global python folder.

PS c:\YOUR_PROJECT_FOLDER> py --version
PS c:\YOUR_PROJECT_FOLDER> py -0p # many python version (3.8.x, 3.9.X, 3.10.x ..) can co-exist without any conflict
PS c:\YOUR_PROJECT_FOLDER> py -m venv venv
PS c:\YOUR_PROJECT_FOLDER> .\venv\Scripts\activate
(venv) PS c:\YOUR_PROJECT_FOLDER> pip list 
Package    Version
---------- -------
pip        20.2.3
setuptools 49.2.1
WARNING: You are using pip version 20.2.3; however, version 21.3 is available.
You should consider upgrading via the 'c:\users\USER\desktop\YOUR_PROJECT_FOLDER\venv\scripts\python.exe -m pip install --upgrade pip' command.

# Here just copy/past that link to update the local pip of your virtual environment 
(venv) PS c:\YOUR_PROJECT_FOLDER> c:\users\USER\desktop\YOUR_PROJECT_FOLDER\venv\scripts\python.exe -m pip install --upgrade pip
Collecting pip
  Using cached pip-21.3-py3-none-any.whl (1.7 MB)
Installing collected packages: pip
  Attempting uninstall: pip
    Found existing installation: pip 20.2.3
    Uninstalling pip-20.2.3:
      Successfully uninstalled pip-20.2.3
Successfully installed pip-21.3

(venv) PS c:\YOUR_PROJECT_FOLDER> pip list
Package    Version
---------- -------
pip        21.3
setuptools 49.2.1

(venv) PS c:\YOUR_PROJECT_FOLDER> pip install <PYTHON_PACKAGE>

Solution 4:[4]

This has something to do with the Windows update.

PS C:\Users\Your Name\AppData\Local\Programs\Python\Python38> ./python -m venv c:\TEMP\py38-venv
Error: Command '['c:\\TEMP\\py38-venv\\Scripts\\python.exe', '-Im', 'ensurepip', '--upgrade', '--default-pip']' returned non-zero exit status 101.

This will fix the issue:

  • Uninstall Python.
  • Install with the Custom option.
  • Use the "Install for all users".

Install for all users

After this it worked fine:

PS C:\Utilities\PythonBase\Python38> .\python -m venv c:\temp\venv-py38
PS C:\Utilities\PythonBase\Python38>

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Miguel Conde
Solution 2 j3rry
Solution 3 cizario
Solution 4 kinshukdua