How do I run a python script from within the IDLE interactive shell?
The following throws an error:
>>> python helloworld.py
SyntaxError: invalid syntax
How do I run a python script from within the IDLE interactive shell?
The following throws an error:
>>> python helloworld.py
SyntaxError: invalid syntax
In Python 3, there is no
execFile
. One can useexec
built-in function, for instance:You can use this in python3:
The IDLE shell window is not the same as a terminal shell (e.g. running
sh
orbash
). 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 theOpen
command from theFile
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 theRun
->Run Module
command (shortcut F5).On Windows environment, you can execute py file on Python3 shell command line with the following syntax:
Below explains how to execute a simple helloworld.py file from python shell command line
We can execute this file on Python3.7 Shell as below:
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')
you can do it by two ways
import file_name
exec(open('file_name').read())
but make sure that file should be stored where your program is running