Python/python3 executes in Command Prompt, but does not run correctly
Asked Answered
A

4

50

I've solved the issue and am posting to save others from my hours of pain.

Problem

On Windows 10,

Python is installed in C:\Python and added to Environment Variables, but will not properly run programs.

Typing python myscript.py in CMD does not run the program but does not produce any errors.

Typing C:\Python\python.exe myscript.py correctly runs the code.

Solution

Navigate to C:\Users\Username\AppData\Local\Microsoft\WindowsApps\python.exe in CMD and type del /f python.exe.

What was wrong

The file located at C:\Users\MyUsername\AppData\Local\Microsoft\WindowsApps\python.exe is viewed before the Environment Variables in PATH. This file just opens the download page for Python in Edge.

You need to delete this file or rename it, but the items in this folder are protected by Microsoft. You get Error 0x80070780 if you try to delete this file with file explorer. In order to delete it you need to navigate to the folder in CMD with administrator privileges and type the above to remove this file.

I hope this helps someone.

Edit

While the solution I originally posted seems to work for people. I would recommend the solution posted by Max:

https://mcmap.net/q/348239/-python-python3-executes-in-command-prompt-but-does-not-run-correctly

Allness answered 13/8, 2019 at 21:22 Comment(3)
You should place the info in Q/A format. (don't answer the question in the question).Effulgence
Just fix your path to put the preferred version ahead of the WindowsApps directory.Liquidate
Do what the others say. This is apparently due to Microsoft being 'helpful' by making it easier to install python. See devblogs.microsoft.com/python/…Perverted
H
50

Just noticed there's an additional python executable in the windowsApp folder too when running where python.

To remove those you can do it in the App execution aliases in windows setting and uncheck the python aliases.

Hwu answered 22/5, 2020 at 15:6 Comment(3)
the prompt is so easily ignored.. ``` $ python --version Python was not found; run without arguments to install from the Microsoft Store, or disable this shortcut from Settings > Manage App Execution Aliases. ```Mundford
This neat trick helped me get rid of python from the microsoft apps folder.Jokester
Does this affect winget since the app installer executables were added there for winget?Bamby
H
23

Here are the screen captures starting with "Start/Settings" in Windows 10, Version 2004, OS build 19041.685, Windows Experience Pack 120.2212.551.0

Settings

Manage app execution aliases

Hinayana answered 31/12, 2020 at 11:35 Comment(3)
for people don't know what Max is telling. He is suggesting us to turn off the "App Installer".Inapproachable
@oldpride: No, what he's suggesting is tor turn of a hack that makes the DOS (yes, that thing from the late 70's that still hasn't been updated) find the correct .exe files through a hack that makes any other tool that tries to find a .exe not work anymore (since they'll just find an invalid 0 byte .exe file which needs some hidden magic to resolve it's actual location). Seriously, this is definitely not one of MS finest moments.Diabetes
@Diabetes what oldpride meant is that the interesting part in the screenshot is the fact that the toggle for App Installer is set to OffPhysiologist
R
16

Putting this Q/A into the right format so the question does not look open/unanswered

Solution from original poster; tested and worked on my system

Solution

In C:\Users\*Username*\AppData\Local\Microsoft\WindowsApps delete python.exe. However, because these files are protected, chances are you will get an error message when trying to delete the file. In that case, you either have to use your administrator account to delete python.exe, or you call a cmd-window with administrator privileges and force delete ''''rm -f C:\Users*Username*\AppData\Local\Microsoft\WindowsApps\python.exe''''

About the solution

python.exe is not the python - executable one would believe it to be, but invokes the download page for python to be opened in the Edge browser or the Microsoft App Store. The question of "who would come up with such a sadistic thing" and "why?." shall remain unanswered at this point. Fact is that calling python executes this faux- python.exe before the PATH Environment Variables are invoked.

Roca answered 1/2, 2020 at 19:56 Comment(3)
Python comes with visual studio, so why use it as an "exe".... whatever that is.Nalda
This enables using python commands from a cmd prompt. Some people coming to windows from Linux are more comfortable with a command prompt interface - at least at first.Hinayana
I was able to delete python.exe and python3.exe using Shift + Delete on Windows.Macrophage
T
0

What works for me was:

in C:\Users\*Username*\AppData\Local\Microsoft\WindowsApps del /f python.exe and del /f python3.exe

then turn off execution alias for python and python 3.

then go to python 3.10.0 installer and download the installer needed for your OS. Open it and select the repair option and when it finish open the installer again and select the remove option.

If you were using chocolatey maybe you should also run this command in cmd/powershell with admin permissions choco uninstall python --version=3.10.0

Teagan answered 20/6, 2023 at 16:34 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.