How to run a python script from IDLE interactive shell?
Solution 1
Python3:
exec(open('helloworld.py').read())
If your file not in the same dir:
exec(open('./app/filename.py').read())
See https://stackoverflow.com/a/437857/739577 for passing global/local variables.
In deprecated Python versions
Python2 Built-in function: execfile
execfile('helloworld.py')
It normally cannot be called with arguments. But here's a workaround:
import sys
sys.argv = ['helloworld.py', 'arg'] # argv[0] should still be the script name
execfile('helloworld.py')
Deprecated since 2.6: popen
import os
os.popen('python helloworld.py') # Just run the program
os.popen('python helloworld.py').read() # Also gets you the stdout
With arguments:
os.popen('python helloworld.py arg').read()
Advance usage: subprocess
import subprocess
subprocess.call(['python', 'helloworld.py']) # Just run the program
subprocess.check_output(['python', 'helloworld.py']) # Also gets you the stdout
With arguments:
subprocess.call(['python', 'helloworld.py', 'arg'])
Read the docs for details :-)
Tested with this basic helloworld.py
:
import sys
if len(sys.argv) > 1:
print(sys.argv[1])
Solution 2
You can use this in python3:
exec(open(filename).read())
Solution 3
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).
Solution 4
EASIEST WAY
python -i helloworld.py #Python 2
python3 -i helloworld.py #Python 3
Solution 5
Try this
import os
import subprocess
DIR = os.path.join('C:\\', 'Users', 'Sergey', 'Desktop', 'helloword.py')
subprocess.call(['python', DIR])
Related videos on Youtube
tshepang
I do software development for a living and as a hobby. My favorite language is Rust, and I've used Python much in the past. My OS of choice is Debian.
Updated on July 04, 2020Comments
-
tshepang almost 4 years
How do I run a python script from within the IDLE interactive shell?
The following throws an error:
>>> python helloworld.py SyntaxError: invalid syntax
-
TerryA almost 11 yearsWhat does
helloworld.py
look like? -
Serial almost 11 yearsyeah it means something is wrong with youre code post youre code!
-
Ned Deily almost 11 yearsNo, not necessarily. Chances are the OP is typing
python helloworld.py
in an IDLE shell window and that doesn't work. -
Terry Jan Reedy about 9 yearsNor would it work in the standard interpreter. This issue has come up before where people mistakenly think that the interpreter prompt is a command-line prompt.
-
Krishna Oza about 9 yearsYou should accept the answer from Ned Deily if that answer your question correctly. This will also help fellow developers to quickly spot the correct answer.
-
Leonard almost 6 yearsEASIEST WAY:
python -i helloworld.py
also works forpython3
-
-
Erica Kane about 9 yearsBut you can't pass in arguments. :(
-
Ned Deily about 9 yearsUnfortunately, no, it's not easy to run a Python file in IDLE while passing in command line arguments. There is a long-standing open issue for IDLE to do so (bugs.python.org/issue5680). One workaround for testing is to manually initialize
sys.argv
at the very beginning of the program, for example, under the usualif __name__ == "__main__"
boilerplate. -
Terry Jan Reedy about 9 years
subprocess.call(r'c:\path\to\something.py')
does not work for me. OSError: [WinError 193] %1 is not a valid Win32 application -
Sergey Nosov about 9 yearsTry this import os import subprocess DIR = os.path.join('C:\\', 'Users', 'Sergey', 'Desktop', 'a.py') subprocess.call(['python', DIR])
-
arsho about 8 yearsThis answer clarifies why we can not run python script using IDLE shell. Thanks @NedDeily
-
ViFI almost 8 yearsCan you please add an example with command line arguments as well.
-
tanweer alam over 7 yearswhat you are doing is loading a module not running from shell. one difference between two is: 1) Loading module your module name name__= name of file 2) run from shell module name _name__="_main"
-
Forever Learner over 5 yearsthanks for the quotation tip, this worked for me. Upvoted.
-
Suncatcher about 5 yearsDoesn't work with Python3 and asker didn't specify Python version explicitly
-
Hugues Fontenelle about 5 yearsAdded
exec
for Python3 -
Dan Nolan over 4 yearsAs of IDLE 3.7.4, you can now run a module with arguments. Use the new
Run
->Run with Customized...
command (shortcut Shift+F5) and a popup will open where you can supply your arguments. Unfortunately it doesn't remember them currently so you'll be pasting them with every run. -
Grasshopper almost 4 yearsimporting a program to run it is not a good option. Even I used it in beginning but now I know about potential problems like it takes away the effeciency of your program because you are importing it (though negligible).
-
Grasshopper almost 4 yearsIt is not a good way to run programs because you are actually importing and not running it. Its contents will be imported and it will be meaningless if you don't want to utilise them.