How to run a python script from IDLE interactive shell?
Master System Design with Codemia
Enhance your system design skills with over 120 practice problems, detailed solutions, and hands-on exercises.
Introduction
If you are using IDLE, the usual way to run a script is not from the shell prompt directly. The normal workflow is to open the file in an editor window and use Run and Run Module, but if you really want to launch a script from the interactive shell, Python also gives you a few explicit options.
The Recommended IDLE Workflow
IDLE has two main windows:
- the interactive shell
- the script editor
For a normal .py file, the recommended steps are:
- Open IDLE.
- Choose
FileandOpen. - Open your script in the editor window.
- Choose
RunandRun Module, or pressF5.
For example, if your file contains:
Running the module sends the output to the IDLE shell window.
This is the best approach because IDLE handles the file as a real script, saves it first if needed, and resets the execution environment more predictably than ad hoc shell tricks.
Running a Script from the Shell Prompt
If your question is specifically about the >>> prompt, use runpy.run_path rather than copy-pasting the file contents manually.
That executes the file located at hello.py and returns a dict containing the global variables created by the script.
If the script is in another folder, use an absolute path or change the working directory first:
This is cleaner than using exec(open(...).read()), which works but is harder to reason about and gives you fewer useful boundaries.
Running a Script as __main__
Some scripts check whether they are the main program:
In that case, use:
Setting run_name="__main__" makes the script behave more like it would from the command line.
Importing the Script as a Module
If the file is designed to expose functions instead of acting like a standalone script, importing it may be better:
This assumes:
- the file is on Python's import path
- the script is written as an importable module
- repeated imports are acceptable
This is often the cleanest pattern for learning and testing because it encourages you to separate reusable functions from top-level execution.
For example:
From the shell, you can then do:
Why exec(open(...).read()) Is Usually a Last Resort
You may see this pattern online:
It does execute the file, but it has a few downsides:
- it is less explicit than
runpy - it runs in the current shell namespace
- debugging side effects can become confusing
It is acceptable for quick experiments, but if you want a clear answer to "run a script from IDLE shell," runpy.run_path is the better default.
Common Pitfalls
The most common mistake is trying to run a script from the shell when the working directory is wrong. If IDLE cannot find the file, confirm the current directory with:
Another issue is expecting imported modules to rerun automatically. After import hello, Python caches the module. If you edit the file and want to reload it in the same session, use importlib.reload.
Beginners also often confuse script output with returned values. runpy.run_path returns a namespace dict, but a normal script may still communicate primarily by printing.
Finally, if you only want to run a saved script, use F5 in the editor window. That is the cleanest and most IDLE-native solution.
Summary
- In IDLE, the standard way to run a script is
Run Modulefrom the editor window. - From the shell prompt, prefer
runpy.run_path("script.py"). - Use
run_name="__main__"when the script depends on main-program behavior. - Import the file as a module when you want reusable functions, not one-off execution.
- Avoid
exec(open(...).read())unless you deliberately want a quick, loose experiment.

