Unable to create process using '...\python.exe' | error in virtual environment - python

I'm unable to use python within the virtual environment. Python works fine outside of the virtual environment. I'm using Python 3.10.2
I keep on getting the error below when trying to run any python commands.
'C:\Users\User\AppData\Local\Programs\Python\Python310\python.exe'
It might be relevant to mention that I was unable to create the virtual environment through the 'python -m venv env' command. Error generated was
Error: Command '['C:\\Users\\User\\Documents\\Python Projects\\PDFtoText\\env\\Scripts\\python.exe', '-Im', 'ensurepip', '--upgrade', '--default-pip']' returned non-zero exit status 101.
I had to add 'without-pip' to the end of the command to create the virtual environment. Weird thing is, I was able to use pip within the virtual environment without having to manually install it.
The path to python is in the environmental variables.
I tried reinstalling python but that did not help.
Lastly, all these errors started occurring after I downloaded Visual Studio Community 2022.

Short answer, I bet you have a space in your Window's account name (say Your Account is where your account is saved so you have C:\Users\Your Account folder, and there is also a text file C:\Users\Your ("Your" being the first part of your user name). MSVS2022 (maybe earlier versions, too) is known to leave this log file which exposes a bug in Python venv's python launcher. Delete this text file, and your problem should be solved.
See my question/answer for more details.

Related

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.
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 with venv.main(args=('.venv',)), debugging line-by-line and noted that it copies Lib\venv\scripts\nt\python.exe from the python install dir to the local .venv\Scripts folder using shutil.copyfile().
If I run the original Lib\venv\scripts\nt\python.exe in command prompt, it runs with a message No 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 error Unable to create process using 'C:\Users\kesh\AppData\Local\Programs\Python\Python310\python.exe' (note that the python.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 standard python binary and verified that this file and its source in venv\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,... where cmdline 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 the cmdline string. Though I replaced my actual account name with kesh it actually comprises two words and a space...
Update #2:
Solution found as posted below
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)

Python script runs from terminal in virtual environment, but fails to find pandas with VS Code play button

I am new to VS Code. I have created a virtual environment and selected that as the interpreter, in which I have run
pip install -r requirements
from the terminal which allowed for pandas to be installed in that virtual environment (I believe based on the docs
From that directory in the terminal, I ran
python myscript.py
It works. Generates the output as it should.
I then try to run the same script in the window above and I get the error, no module named pandas. I have not changed the interpreter.
UPDATE
When closing the terminal and then attempting to run the code from the explorer/deug window, I received a failure to run scripts notice. I therefore (based on this post changed my Execution Policies in Windows Shell (separate, not in VS terminal), and now it at least goes into the environment, however I still get the same error, pandas unknown.
UPDATE 2
I noticed in the tutorial that they created the directory and then created the environment within that. I moved my code to be under the virtual environment I created and then it works, both from the terminal and from the debug/explorer window. However here I noticed that the intrepetor is not characterised as a virtual environment but instead looks like the standard version- now there is no option for me to select working_env as an interpretor.
Running pip freeze shows the latest version of pandas which is not what I have installed elsewhere for instance. So this works, but I am now not sure if this is really how I should be using virtual environments.
**UPDATE 3
For whatever reason I can now update the interpreter. When I do however I am back to where I was. I can run the program in the terminal with the interpreteter selected, but running from the debug window I get the error that pandas is missing. Trying to install pandas I am told the requirement is satisfied. Running pip freeze confirms that this is installed. I have the feeling I am somehow installing pandas not in the environment I want
What have I missed in my vscode setup?
Per both the comments, I had a couple of issues here.
For whatever reason during update 2, I couldn't see the interpreter when I was in the folder initially but it was there. It is noted in the docs that this can take a while. Regardless, it must show in the bottom left that you are in the interpreter
Using powershell, I needed to change the execution policy to be unrestricted.
Set-ExecutionPolicy Unrestricted -Scope CurrentUser
Then I hadn't installed pandas in the virtual environment AND with the interpreter. Therefore
. ..\Scripts\activate.ps1
activated the working environment
python -m pip install -r requirements.txt
installed my packages
Running my script was achieved by
python .\myscript.py
and that also runs with the play button (though here I had to press Ctrl+Sft+P type Terminal: Select Default Shell, ensure the terminal was powershell & restart)

Visual Studio Code not opening any python interpret

I am having an issue with opening and running python on VS Code. In particular, I cannot open any python interpret regardless of having python installed on the computer and the python extension in VS code.
1 week ago, there was no issue with that, I opened VS Code, followed the instructions and python was running (however without any packages or libraries). Because I use python for Data Science projects, I decided to integrate Jupyter notebook - here again, no issues.
Afterwards, I got the idea that I should be able to import libraries and run codes in file_name.py, not only file_other_name.ipynb, so I was trying to create an environment in my project folder that will store installed packages by typing py -3 -m venv .venv .venv\scripts\activate and python -m pip install matplotlib in the terminal. Unfortunately, that did not work when running a basic script - getting a message "Activate.ps1 is not digitally signed. You cannot run this script on the current system."
Hence, I set up the powershell to Set-ExecutionPolicy -ExecutionPolicy Unrestricted. Once, I restarted VS Code, the current environment started showing a message 'Select Python Interpret'. I tried to reinstall the python extension and select from the Command Palette (Ctrl + Shift + P) any of the python interprets, that it is giving me but nothing happens. The message does not change regardless of how many times I have specified which interpret to open. Any idea how I can open the python interpret once again?
enter image description here
There's a default setting called "python.terminal.activateEnvironment": true, so every time you create an virtual environment and select it as your interpreter, don't forget to open a new integrated Terminal(Ctrl+Shift+`) to activate it.
[EDIT]
When you open a new integrated Terminal, there'll be an statement executed automatically: that's a file in your created .venv, which is by red underlined in the following screenshot. Then you get a virtual name prefix before PS, that means you've activated it successfully.
If the Activate.ps1 isn't executed automatically, turn to the .venv folder, copy its absolute path then run it in Terminal still can activate the environment.
Then go on development, like installing the required module:

why am i getting (gsutil): "C:\Users\user\AppData\Local\Programs\Python\Python37\python.exe": command not found

After installing Google cloud sdk and connecting to desired firebase project i am receiving :
ERROR: (gsutil)
"C:\Users\user\AppData\Local\Programs\Python\Python37\python.exe":
command not found when running any gsutil command.
My current stup is:
windows 10
Google Cloud SDK 281.0.0
bq 2.0.53
core 2020.02.14
gsutil 4.47
python 3.7
My theory is, that while installed "correctly" python doesnt have access to gsutil commands
I had the same problem and I was able to solve it by setting a new environment variable for CLOUDSDK_PYTHON. On windows 10 you can do this from the command line in 2 ways:
Set an env variable for the current terminal session
set CLOUDSDK_PYTHON="C:\Users\user\AppData\Local\Programs\Python\Python37\python.exe"
Set a permanent env variable
setx CLOUDSDK_PYTHON="C:\Users\user\AppData\Local\Programs\Python\Python37\python.exe"
The file path will probably be different for everyone, so check first where is python.exe located and use your own path. I hope this helps.
Run:
set CLOUDSDK_PYTHON=C:\Users\user\AppData\Local\Programs\Python\Python37\python.exe
Note: There should be no quotes around the python path like this "C:\Users\user\AppData\Local\Programs\Python\Python37\python.exe" or it would attempt to run the command with quotes, which we know won't work.
To see a list of components that are available and currently installed, run command:
gcloud components list
To update all installed components to the latest available version(282.0) of Cloud SDK, run command:
gcloud components update
You also can reinstall it following this document, while Cloud SDK currently uses Python 2 by default, you can use an existing Python installation if necessary by unchecking the option to 'Install Bundled Python'.
As was suggested above reinstalling using bundled python worked for me. I had incorrectly assumed from google's doc i should choose between bundled or current python install not realizing both could run without conflict.
Syntax needed to be a little different for me in CMD and/or PowerShell - also I installed Python via the Microsoft Store so the command for me was:
SETX CLOUDSDK_PYTHON "C:\Program Files\WindowsApps\PythonSoftwareFoundation.Python.3.9_3.9.1520.0_x64__qbz5n2kfra8p0\python3.9.exe"
you can get the exact path by running the python app from the start menu and then reading the window title:

Python 3 ModuleNotFoundError: No module named 'numpy'

I trying to run a program in python 3 that uses numpy but it gives me the error ModuleNotFoundError: No module named 'numpy'. I am using Windows 10. I tried running pip install numpy and it says pip is not a recognized command
You need to add python to your environment variables
Computer -> System Properties (or Win+Break) -> Advanced System Settings
Click the Environment variables button (in the Advanced tab)
Edit PATH and append ;C:\Python27 to the end (if you need substitute your Python version)
Click OK. Note that changes to the PATH are only reflected in command prompts opened after the change took place.
If you are running python 3.6 you will need to add python to your environment path through command prompt
Windows allows environment variables to be configured permanently at both the User level and the System level, or temporarily in a command prompt.
To temporarily set environment variables, open Command Prompt and use the set command:
set PATH=C:\Program Files\Python 3.6;%PATH%
set PYTHONPATH=%PYTHONPATH%;C:\My_python_lib
These changes will apply to any further commands executed in that console and will be inherited by any applications started from the console.
To change System variables, you need non-restricted access to your machine (i.e. Administrator rights).
If you were to run python -v it should now work
This came directly from the python documents section 3.3.1
Now after you restart you should be able to access python through the command line. If you are running 2.7.9+ or 3.4+ pip will come prepackaged with python. However, if you are running an earlier version of python
Per https://pip.pypa.io/en/stable/installing/#do-i-need-to-install-pip:
Download get-pip.py, being careful to save it as a .py file rather than .txt. Then, run it from the command prompt:
python get-pip.py
You possibly need an administrator command prompt to do this. Follow Start a Command Prompt as an Administrator (Microsoft TechNet).
Thanks to How do I install pip on Windows?
Add "way to folder with your python interpreter" and "way to folder with your python interpreter"\Scripts\ to PATH variable. Computer -> Properties -> Extra options -> Environment variables.

Categories

Resources