16

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.

aschipfl
  • 31,767
  • 12
  • 51
  • 89
Bob
  • 323
  • 1
  • 2
  • 10
  • 1
    are you sure that `idle.bat` can run with arguments and it can open file in IDLE ? Did you try in console/cmd.exe `idle.bat script.py` to run `script.py` in IDLE ? – furas Jan 13 '17 at 16:59
  • 3
    sounds like python isn't in your PATH – Nick stands with Ukraine Jan 13 '17 at 17:00
  • 2
    Possible duplicate of [Python scripts stopped running on double-click in Windows](http://stackoverflow.com/questions/20521456/python-scripts-stopped-running-on-double-click-in-windows) – David Cullen Jan 13 '17 at 17:02
  • 1
    In Windows, you should right-click and select "Open with", then choose (or search) python.exe and check the "remember" box – Don Jan 13 '17 at 17:03
  • Yes it can open files in IDLE it did. It opened python file in IDLE Editor on doubleclick. – Bob Jan 13 '17 at 17:04
  • @Don but what file i should choose? I choosed idle.bat and it doesnt run the python file it only opens it in IDLE Editor – Bob Jan 13 '17 at 17:04
  • 1
    BTW As soon as you get practice with virtualenvs, you wouldn't want to run a Python script with double-click – Don Jan 13 '17 at 17:05
  • 1
    Current 3.x Windows installers install Python so that double clicking runs the file. I am not sure if adding the python dir to path is required or not, but it may be. With multiple pythons installed, `py -x.y program.py` in a console lets one select from multiple installed pythons. If program.py is in a directory on sys.path, making it importable, `py -x.y -m program` works regardless of the current directory. idle.bat is a different subject. It is for running IDLE with preset arguments other than the defaults. – Terry Jan Reedy Jan 13 '17 at 23:20
  • 1
    Possible duplicate of [Making a Python script executable](http://stackoverflow.com/questions/26045113/making-a-python-script-executable) – MackM Feb 23 '17 at 14:39

5 Answers5

24

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.

OregonTrail
  • 7,916
  • 6
  • 38
  • 56
7

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

ratna
  • 71
  • 1
  • 1
5

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.

Xiidozen
  • 83
  • 7
1

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.

James Koss
  • 523
  • 5
  • 10
0

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:\temp\manage.py runserver