37

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.

Wiktor_B
  • 395
  • 1
  • 4
  • 8

8 Answers8

66

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"

Miguel Conde
  • 813
  • 10
  • 22
11

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

sudo python3.8 -m venv venv
desertnaut
  • 57,590
  • 26
  • 140
  • 166
j3rry
  • 145
  • 1
  • 1
  • 6
  • 2
    Wow, I've been pulling my hair out for the last 3 hours trying to figure this out and never thought to try sudo.. Needless to say this worked me for me. – Michael Skarn May 03 '22 at 17:10
  • "sudo python3 -m venv my_env" this also works for WSL, Kali linux. – Akash Shendage Jun 08 '22 at 06:19
  • That's awesome! I literally had root enabled, but when I use `sudo` it's worked like magic. I really wonder why is that. (I'm not running Linux inside windows) – iTzVoko Dec 02 '22 at 20:30
  • using sudo to create a basic virtual env. what the heck – WhyWhat Feb 02 '23 at 22:13
  • I know we shouldn’t use sudo on everything, but it does work in this situation. I didn’t find any other solution. – j3rry Feb 04 '23 at 00:32
  • my guess is it is not because it requires elevated privileges, its more likely because the first user references an alias that is set. root (the user when you use sudo) has it's own .bashrc file and therefore it's own understanding of what the command means. All of that to say, if you don't want to have to use sudo for this command, try comparing your user .bashrc (or .bash_aliases) files with the root. – Rob S. Feb 08 '23 at 15:55
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>
cizario
  • 3,995
  • 3
  • 13
  • 27
2

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>
kinshukdua
  • 1,944
  • 1
  • 5
  • 15
2

TLDR: On Cygwin install python-setuptools-wheel and python-pip-wheel packages.

To find the specific reason I installed a venv without pip support first (python3 -m venv venv --without-pip) loaded it and ran the failing command (python -Im ensurepip --upgrade --default-pip) manually. This generated a stack trace suggesting that it iterates over all versions of the setuptools wheel but none is actually installed:

  Traceback (most recent call last):
  File "/usr/lib/python3.9/runpy.py", line 188, in _run_module_as_main
    mod_name, mod_spec, code = _get_module_details(mod_name, _Error)
  File "/usr/lib/python3.9/runpy.py", line 147, in _get_module_details
    return _get_module_details(pkg_main_name, error)
  File "/usr/lib/python3.9/runpy.py", line 111, in _get_module_details
    __import__(pkg_name)
  File "/usr/lib/python3.9/ensurepip/__init__.py", line 30, in <module>
    _SETUPTOOLS_VERSION = _get_most_recent_wheel_version("setuptools")
  File "/usr/lib/python3.9/ensurepip/__init__.py", line 27, in _get_most_recent_wheel_version
    return str(max(_wheels[pkg], key=distutils.version.LooseVersion))
ValueError: max() arg is an empty sequence

After installing python-setuptools-wheel it generates the same stack trace but with _get_most_recent_wheel_version("pip") -- hence python-setuptools-pip is also missing. After installing that as well the command works and generating a venv with pip support also works.

Konrad
  • 509
  • 5
  • 17
  • Thank you! This worked for me under cygwin in Windows 10. – riopiedra Mar 02 '23 at 14:31
  • seriously, just curious, with the advent of WSL and WSL2, why is anyone still using cygwin?? – Daniel Goldfarb Apr 03 '23 at 15:21
  • Last time I tried WSL was heavier and slower on startup time and did not integrate into Windows as good as Cygwin does - at least for me and my workflow. Unfortunately it's been some time and I don't remember the details. (One minor point was the lack of support for USB Serial adapters.) – Konrad Apr 04 '23 at 09:48
1

Simply i run this virtualenv venv

Because i installed the venv like this pip install virtualenv

Mouaad
  • 106
  • 1
  • 4
0

In order to create a virtual environment, Python requires some packaging tools to be installed. The packaging tools are found in python3-setuptools.

Installing the Python3 setup tools should correct this error.

sudo apt install python3-setuptools
desertnaut
  • 57,590
  • 26
  • 140
  • 166
Justin
  • 101
  • 3
-1

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

This error sometimes occurs because of the antivirus, you can solve it by simply disabling it when using the command.

  • Your answer could be improved with additional supporting information. Please [edit] to add further details, such as citations or documentation, so that others can confirm that your answer is correct. You can find more information on how to write good answers [in the help center](/help/how-to-answer). – Community Apr 07 '23 at 00:26