How to execute .py file with double-click on Windows

Question:

I just uninstalled and reinstalled python on my Windows machine. Before I uninstalled my previous version I was able to just double-click on a python script and it would open the command prompt, run the script, and close automatically. After re-installing with the newest version (3.9), I am no longer able to execute the script like that with a double-click.

Clearly I had done something special last time to set that up for myself, but I don’t remember what it was. Any idea how I can get that double-click deal going again?

Asked By: Lycheki

||

Answers:

There will be an option of "Open With" after right-click on the file go and choose CMD. I hope it helps if not then sorry. Because I use Parrot OS

Answered By: KGB

Doing the following should fix it:

  1. Right click on the .py file you want to open;
  2. Open with -> Choose default program -> More options;
  3. Select the python.exe file.

Explanation:

Your Python scripts have to be processed by another program called the Python interpreter. The interpreter reads your script, compiles it into bytecodes, and then executes the bytecodes to run your program.

Installing a new version might have messed the path to the Python interpreter. The steps listed above will tell Windows to associate .py files with your Python interpreter, thus fixing the issue.

This link with Python on Windows FAQ might also be of help.

Answered By: Talendar

Save the following text to a file called something like python.reg (the .reg extension is important). You might need to modify the last line to be your exact path to python.exe!

Windows Registry Editor Version 5.00

[HKEY_CLASSES_ROOTPython.Fileshellopen]

[HKEY_CLASSES_ROOTPython.Fileshellopencommand]
@=""C:\Python2.7\python.exe" "%1" %*"

Find the python.reg file you just saved and double-click it to load those contents into your Registry. If you’ve performed one of the other operations in other answers (like "Open With"), those "UserChoice" settings are stored somewhere else in the Registry and will override the "Classes" setting shown in this script. So, do one or the other, don’t combine them!

Answered By: jimtut
Categories: questions Tags: , ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.