Running Python file by double-click

Question:

I have really annoying problem, I cannot run a Python file just by double-clicking.

I have tried to set it to open the file with idle.bat but that only starts IDLE editor on double-click, it does not run the Python file.

Asked By: Bob

||

Answers:

What version of Python do you have installed?

You should write your own batch file to execute your python binary and your script.

For example, with a default Python 2.7 installation on Windows, this could be the entire contents of your script.

myscript.bat:

ECHO ON
REM A batch script to execute a Python script
SET PATH=%PATH%;C:Python27
python yourscript.py
PAUSE

Save this file as “myscript.bat” (make sure it’s not “myscript.bat.txt”), then double click it.

Answered By: OregonTrail

Right click the file, select open with. If you want to simply run the script, find python.exe and select it. If you want to debug with IDLE, find that executable and select it.

Answered By: Xiidozen

right click on the file->open with->choose default program->more options->select python.exe file and click on.

Answered By: ratna

When I had both Py2 and Py3, and then removed the former, my script wouldn’t run by double-clicking it either (but fine from console.) I realized my __pycache__ folder (same directory as the script) was the issue. Problem solved when deleted.

Answered By: James Koss

You can also start a Django app this way. Once the Django server starts it enters a "wait" kind of mode so a batch file only requires two lines:

ECHO ON
python manage.py runserver

Manage.py can be in any directory, just keep the full folder path in the command within the batch file:

ECHO ON
python C:tempmanage.py runserver

Answered By: ezst036

Solution for Ubuntu users.
Right-click on the file, then select open with, then choose your python version, it shode be in
/bin folder, usually it’s /bin/python3.exe

Answered By: Alex Katarani

In Windows 10, using regedit:

  • Under HKEY_CLASSES_ROOT, create key: .py
  • Edit Default string value to be py_auto_file (or any other name you want to call it)
  • Under HKCR, create another key: py_auto_file (or any matching name that you have just picked)
  • Under this key, create nested sub-keys: shell –> open –> command.
  • Edit Default string value to "C:pathtoyourpython.exe" "%1"
Answered By: Yvon
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.