kesh
kesh

Reputation: 5463

Windows Python (<=3.10.2) fails to run `python -m venv .venv`

This issue has been solved, resulted in a bug report to Python.org. See the my self-answer below for the workaround until it's fixed in a future release of Python

One of my PCs got bitten by this bug which no longer allows me to create venv with the error:

Error: Command '['C:\\Users\\kesh\\test\\.venv\\Scripts\\python.exe', '-Im', 'ensurepip', '--upgrade', '--default-pip']' returned non-zero exit status 101.

I'm trying to figure out what exactly is happening, but quickly running out of ideas. Here are what I've tried so far:

If you have any other ideas to try, please share.

Update #1:

Update #2:

Solution found as posted below

Upvotes: 8

Views: 9925

Answers (2)

DisALL
DisALL

Reputation: 1

I encountered the same problem in python3.8.10 from pyenv-win. How to solve it?

2024-05-25 12:33:04.266 [info] > "D:\Program Files\python\pyenv-win\versions\3.8.10\python.exe" ~\.vscode\extensions\ms-python.python-2024.4.1\python_files\create_venv.py --git-ignore
2024-05-25 12:33:04.266 [info] cwd: .
2024-05-25 12:33:04.495 [info] Running: D:\Program Files\python\pyenv-win\versions\3.8.10\python.exe -m venv .venv
2024-05-25 12:33:04.705 [info] Error: Command '['e:\\Python\\chatgpt-on-wechat\\.venv\\Scripts\\python.exe', '-Im', 'ensurepip', '--upgrade', '--default-pip']' returned non-zero exit status 101.
2024-05-25 12:33:04.712 [info] Traceback (most recent call last):
  File "d:\Users\li\.vscode\extensions\ms-python.python-2024.4.1\python_files\create_venv.py", line 84, in run_process
2024-05-25 12:33:04.712 [info]     subprocess.run(args, cwd=os.getcwd(), check=True)
  File "D:\Program Files\python\pyenv-win\versions\3.8.10\lib\subprocess.py", line 516, in run
2024-05-25 12:33:04.712 [info]     raise CalledProcessError(retcode, process.args,
subprocess.CalledProcessError: Command '['D:\\Program Files\\python\\pyenv-win\\versions\\3.8.10\\python.exe', '-m', 'venv', '.venv']' returned non-zero exit status 1.

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "d:\Users\li\.vscode\extensions\ms-python.python-2024.4.1\python_files\create_venv.py", line 250, in <module>
2024-05-25 12:33:04.713 [info]     main(sys.argv[1:])
  File "d:\Users\li\.vscode\extensions\ms-python.python-2024.4.1\python_files\create_venv.py", line 219, in main
2024-05-25 12:33:04.714 [info]     run_process(
  File "d:\Users\li\.vscode\extensions\ms-python.python-2024.4.1\python_files\create_venv.py", line 86, in run_process
    raise VenvError(error_message)
__main__.VenvError: CREATE_VENV.VENV_FAILED_CREATION
2024-05-25 12:33:04.729 [error] Error while running venv creation script:  CREATE_VENV.VENV_FAILED_CREATION
2024-05-25 12:33:04.729 [error] CREATE_VENV.VENV_FAILED_CREATION
2024-05-25 12:33:05.870 [info] > .\.venv\Scripts\python.exe -I ~\.vscode\extensions\ms-python.python-2024.4.1\python_files\get_output_via_markers.py ~\.vscode\extensions\ms-python.python-2024.4.1\python_files\interpreterInfo.py
2024-05-25 12:33:05.920 [error] [Error: Command failed: e:\Python\chatgpt-on-wechat\.venv\Scripts\python.exe -I d:\Users\li\.vscode\extensions\ms-python.python-2024.4.1\python_files\get_output_via_markers.py d:\Users\li\.vscode\extensions\ms-python.python-2024.4.1\python_files\interpreterInfo.py
Unable to create process using 'D:\Program Files\python\pyenv-win\versions\3.8.10\python.exe -I d:\Users\li\.vscode\extensions\ms-python.python-2024.4.1\python_files\get_output_via_markers.py d:\Users\li\.vscode\extensions\ms-python.python-2024.4.1\python_files\interpreterInfo.py'

Upvotes: 0

kesh
kesh

Reputation: 5463

Bingo, the finding in the update #1 was the cause. The space in my username was the culprit. Although I have no idea what triggered this behavior change on my account... (anybody with an answer, please follow up.)

Let's say the per-user python is installed at

C:\Users\User Name\AppData\Local\Programs\Python\Python310

In my case, "Microsoft Visual C++ 2015-2022 Redistributable" installer (VC_redist.x64.exe) left a log file C:\Users\User (a text file with the first part of my account name as its file name). This caused python venv to use C:\Users\User as the python executable and promptly failed (see the issue tracker link below for the full explanation).

You can fix the issue in 2 ways until Python patches the problem.

Easy Fix

Simply delete the file C:\Users\User

Note: This will work until next time another installer leaves this nasty log file.

More Involved Fix

In command console, run

DIR /X C:\Users

which lists something like:

02/08/2022  11:44 AM    <DIR>                       .
02/08/2022  11:44 AM    <DIR>                       ..
11/19/2020  01:48 AM    <DIR>                       Public
02/08/2022  02:47 PM    <DIR>          USERNA~1     User Name

Open the Environmental Variables dialog and edit Path user variable's Python entries from

C:\Users\User Name\AppData\Local\Programs\Python\Python310\Scripts
C:\Users\User Name\AppData\Local\Programs\Python\Python310

to

C:\Users\USERNA~1\AppData\Local\Programs\Python\Python310\Scripts
C:\Users\USERNA~1\AppData\Local\Programs\Python\Python310

Re-open python console window or app so the path change is applied to your dev environment.

Note: This fix will work as long as you don't update python version. When you do, you need to delete the old path entries manually and update the new path entries.

Eventual Fix

I reported this bug to python bug tracker: Issue 46686. They've acknowledged the bug and have it labeled as critical with a proposed fix. So, hopefully it will get fixed in a near future release. (>3.10.2)

Upvotes: 18

Related Questions