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.
- This issue has been known, chronologically: v3.7.2, v3.8, v3.?, & v3.10.1
- The only known solution is to give up per-user install and use global install by checking "Install for all users" option
I'm trying to figure out what exactly is happening, but quickly running out of ideas. Here are what I've tried so far:
- On my PC, "Install for all users" works as well as per-user install on a dummy account (all using the same v3.10.2 installer). This singles out the issue to be on my Windows account. Changing the install location does not help.
- Went into
venv
source by running Python withvenv.main(args=('.venv',))
, debugging line-by-line and noted that it copiesLib\venv\scripts\nt\python.exe
from the python install dir to the local.venv\Scripts
folder usingshutil.copyfile()
. - If I run the original
Lib\venv\scripts\nt\python.exe
in command prompt, it runs with a messageNo pyvenv.cfg file
(which makes sense as the .cfg file is in.venv
folder which it couldn't see) - If I call the copied
.venv\Scripts\python.exe
then it returns an errorUnable to create process using 'C:\Users\kesh\AppData\Local\Programs\Python\Python310\python.exe'
(note that thepython.exe
path for the process is that of the installed Python exe) - If
.venv
is installed successfully (on the dummy Windows account), the above run starts a Python session as you'd expect. venv\scripts\nt\python.exe
is different from the standardpython
binary and verified that this file and its source invenv\Scripts\nt
are identical.- All this points to that something in my account configuration is bothering the
.venv\Scripts\python.exe
to do the right thing, but my environmental variables are pretty clean and python paths are at the top of the user PATH variable. - Currently trying to locate the source code of
.venv\Scripts\python.exe
but not found it yet. - Can it be something in registry?
If you have any other ideas to try, please share.
Update #1:
- Found the source of the error message
PC/launcher.c
Line 814 - Possibility:
CreateProcessW(NULL, cmdline,...
wherecmdline
is the original python path in the error message, without quote. CreateProcessW documentation states executable name is deduced from the first white space–delimited token in thecmdline
string. Though I replaced my actual account name withkesh
it actually comprises two words and a space...
Update #2:
Solution found as posted below