Search code examples
pythonwindowspython-2.7shellpython-idle

How to run a script in Idle shell when double-clicked in Window OS?


Summary:

Windows User, that has python installed on his/her machine, double-clicks a (.py) or (.bat) file and it launches myapp.py in the Python Idle Shell.

Alternatively, the myapp.py is able to launch itself in Python Idle Shell after getting double clicked but that is probably not feasible.

More rambly (to prevent misunderstanding):

If someone wants to run a (.py) file via Python Idle Shell, they would right-click on the file > 'Edit with IDLE' > 'Run Module (F5)'. How can I run the (.py) file via Python Idle Shell with just double-clicking a (.py) file or (.bat) file?

Additional info

OS: Windows 7 (x64)

Python: 2.7


Solution

  • As I said in a comment, from the Command line usage section of the Python 2 Idle documentation it sounded like you could do it in a batch file by starting idle and passing it the -r file option to get it to run your script. (Note for Python 3, here's the Command line usage section.)

    Today I had a chance to explore this further myself and can report that it does indeed appear to work. So now here's what I meant in more concrete terms.

    The following batch file is what I used on my Windows 7 (x64) system with the 32-bit version Python 2.7.14 installed. You will likely have to tweak the file paths used in it to suit your own configuration.

    idletest.bat

    @echo on
    python "C:\Python\Lib\idlelib\idle.py" -r "idletest.py"
    pause