Advertisement
Advertisement


How to run a python script from IDLE interactive shell?


Question

How do I run a python script from within the IDLE interactive shell?

The following throws an error:

>>> python helloworld.py
SyntaxError: invalid syntax
2014/02/16
1
103
2/16/2014 4:46:50 PM


You can use this in python3:

exec(open(filename).read())
2016/12/01

The IDLE shell window is not the same as a terminal shell (e.g. running sh or bash). Rather, it is just like being in the Python interactive interpreter (python -i). The easiest way to run a script in IDLE is to use the Open command from the File menu (this may vary a bit depending on which platform you are running) to load your script file into an IDLE editor window and then use the Run -> Run Module command (shortcut F5).

2013/06/22

Try this

import os
import subprocess

DIR = os.path.join('C:\\', 'Users', 'Sergey', 'Desktop', 'helloword.py')

subprocess.call(['python', DIR])
2015/03/15

execFile('helloworld.py') does the job for me. A thing to note is to enter the complete directory name of the .py file if it isnt in the Python folder itself (atleast this is the case on Windows)

For example, execFile('C:/helloworld.py')

2014/10/10

EASIEST WAY

python -i helloworld.py  #Python 2

python3 -i helloworld.py #Python 3
2018/08/02