Lesson 3: Running Python: IDLE, interactive mode, script files and the command line
There are three ordinary ways to run Python: type lines into IDLE or a terminal and see the answer immediately, or save a file and run it. This lesson shows all three and when each one is the right choice.
In short
- Interactive mode (the >>> prompt) runs one line at a time and forgets it when you close the window.
- Script mode saves code in a `.py` file, which can be edited, kept and run again.
- IDLE is the small editor that comes with Python: type a file, press F5, see the output.
- On the command line, `python hello.py` runs a saved file; `python` alone opens interactive mode.
3
ways to run Python: interactive, script file, IDLE
this lesson
0
extra software needed for any of them
Python installer
Three ways to run the same language
An interpreter is the program that reads Python code and carries it out. You can talk to it in two ways: one line at a time, or through a file it reads from top to bottom.
1. Interactive mode
Type python in the command prompt (or python3 on Linux) and you get a prompt made of three greater-than signs:
>>> 2 + 3
5
>>> name = "Asha"
>>> print("Hello", name)
Hello Asha
>>> exit()
Each line is executed as soon as you press Enter, and the answer is printed for you. This is also called a REPL: read, evaluate, print, loop. It is the fastest way to test one idea, and the worst way to write a program, because nothing is saved.
2. Script mode: a saved file
Put the same instructions in a file ending in .py. Nothing appears until you run the file, and once it finishes you are back at the command prompt.
# hello.py
print("Hello from a file")
print("This line runs after the first one")
for i in range(3):
print("Line", i + 1)
python hello.py
Hello from a file This line runs after the first one Line 1 Line 2 Line 3
This is the normal way to write programs: a file can be edited, copied, submitted and run again tomorrow.
3. IDLE
IDLE stands for Integrated Development and Learning Environment. It is the editor installed with Python, and it offers both modes in one window.
- Open IDLE from the Start menu, or type
idlein a terminal. - The window that opens is the shell: the same
>>>prompt as interactive mode. - For a program, choose File → New File, type the code, and save it as
hello.py. - Press F5 (Run → Run Module). IDLE runs the file in its shell and shows the output.
On a Windows machine
- Keep programs in a short folder such as
C:\python. Long paths with spaces cause trouble in the command prompt. - Open the command prompt with Win + R, type
cmd, press Enter. Thencd C:\pythonmoves into your folder. - Run a file with
python hello.py. If that opens the Microsoft Store instead, Python is not properly installed: install it from python.org and try again. - If two versions are installed,
py hello.pypicks the newest one, andpy -3.10 hello.pypicks exactly 3.10. - In IDLE, File → Save As writes the file, and F5 runs it in the shell window.
Which one should you use
| Use | Best choice | Why |
|---|---|---|
| Test one line or one expression | Interactive mode | No file to create, answer appears at once. |
| Write a program you will keep | Script file in IDLE or an editor | The code is saved and can be run again. |
| Run a program repeatedly from a terminal | python file.py | Fast, and easy to combine with other commands. |
Things that go wrong at this stage
- Nothing happens when you run a file. The file only had definitions in it; a program needs at least one
print()or another action. - SyntaxError pointing at a line. Usually a missing bracket, quote or colon. The line number in the message is the place to look.
- The window closes before you can read the output. Run from a terminal, or run the file inside IDLE so the shell stays open.
- The file runs an old version. You saved it under another name, or edited a copy in a different folder. Check the path in the window title.
Practice
Try these before moving on. Each one uses only what this lesson covered.
- Print your name in interactive mode, then do the same from a saved file `myname.py`.
- Save a file that prints two lines and run it with `python myname.py` from the command prompt.
- Predict the output of `print(10 / 4)` and `print(10 // 4)` before running them in interactive mode.
Sources
Frequently asked questions
What is the difference between interactive mode and script mode?
Interactive mode runs one line at a time and keeps nothing after you close it. Script mode runs a whole saved file from top to bottom, which is how real programs are written and kept.
What is IDLE in Python?
IDLE is the editor that comes with Python. It has a shell for interactive lines and an editor window for files, and F5 runs the file you are editing.
How do I run a Python file from the command line?
Open a terminal in the folder that contains the file and type python hello.py (python3 hello.py on many Linux systems). The program runs and prints its output in that terminal.
Why does nothing print when I run my file?
Because the file only defines things. A definition such as a function or a variable does not produce output by itself; you have to call it or print it.