Python
IDLE
Interactive Shell
Run Script
Programming Tips

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.

IDLE has two main windows:

  • the interactive shell
  • the script editor

For a normal .py file, the recommended steps are:

  1. Open IDLE.
  2. Choose File and Open.
  3. Open your script in the editor window.
  4. Choose Run and Run Module, or press F5.

For example, if your file contains:

python
1print("Hello from IDLE")
2
3for n in range(3):
4    print(n)

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.

python
import runpy

runpy.run_path("hello.py")

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:

python
1import os
2import runpy
3
4os.chdir("/Users/markqian/projects/demo")
5runpy.run_path("hello.py")

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:

python
if __name__ == "__main__":
    print("Running as a script")

In that case, use:

python
import runpy

runpy.run_path("hello.py", run_name="__main__")

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:

python
import hello

hello.main()

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:

python
1def greet(name):
2    return f"Hello, {name}"
3
4def main():
5    print(greet("IDLE"))
6
7if __name__ == "__main__":
8    main()

From the shell, you can then do:

python
import hello
print(hello.greet("Mark"))
hello.main()

Why exec(open(...).read()) Is Usually a Last Resort

You may see this pattern online:

python
exec(open("hello.py").read())

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:

python
import os
print(os.getcwd())

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 Module from 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.

Course illustration
Course illustration

All Rights Reserved.