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
venvsource by running Python withvenv.main(args=('.venv',)), debugging line-by-line and noted that it copiesLib\venv\scripts\nt\python.exefrom the python install dir to the local.venv\Scriptsfolder usingshutil.copyfile(). - If I run the original
Lib\venv\scripts\nt\python.exein command prompt, it runs with a messageNo pyvenv.cfg file(which makes sense as the .cfg file is in.venvfolder which it couldn't see) - If I call the copied
.venv\Scripts\python.exethen it returns an errorUnable to create process using 'C:\Users\kesh\AppData\Local\Programs\Python\Python310\python.exe'(note that thepython.exepath for the process is that of the installed Python exe) - If
.venvis installed successfully (on the dummy Windows account), the above run starts a Python session as you'd expect. venv\scripts\nt\python.exeis different from the standardpythonbinary and verified that this file and its source invenv\Scripts\ntare identical.- All this points to that something in my account configuration is bothering the
.venv\Scripts\python.exeto 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.exebut 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.cLine 814 - Possibility:
CreateProcessW(NULL, cmdline,...wherecmdlineis 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 thecmdlinestring. Though I replaced my actual account name withkeshit actually comprises two words and a space...
Update #2:
Solution found as posted below