Quick answer: Clearing a terminal screen is different from resetting Python state. Use the frontend’s clear command for visible output, restart the interpreter when variables and imports must disappear, and use the notebook or IDE’s own output controls when Python is embedded there.

To clear the Python shell, use the command that matches where you are running Python. In most terminal-based REPL sessions, Ctrl+L clears the visible screen. In Windows Command Prompt or PowerShell, use cls. In macOS and Linux terminals, use clear. From inside Python code, call a fixed operating-system command with os.system().
Clearing the shell does not delete command history or reset variables. It only cleans the visible output area. If you want a clean Python environment, restart the interpreter instead.
Clear the Python Shell Manually
Use these commands outside Python, directly in your terminal:
| Environment | Clear command |
|---|---|
| Windows Command Prompt | cls |
| Windows PowerShell | cls or Clear-Host |
| macOS Terminal | clear or Ctrl+L |
| Linux Terminal | clear or Ctrl+L |
If Python itself is not opening from the terminal, fix that first with Python Pool’s guide to Python is not recognized as an internal or external command.
Clear the Screen From Python Code
For a small script that works on Windows and Unix-like systems, choose the command from os.name:
import os
def clear_python_shell():
command = "cls" if os.name == "nt" else "clear"
os.system(command)
for number in range(5):
print(number)
input("Press Enter to clear the screen...")
clear_python_shell()The official os.name documentation identifies the imported operating-system module, and os.system() executes a command string in a subshell. Keep the command fixed, as shown above. Do not pass untrusted user input into os.system().
For a broader look at shell commands from Python, see Python shell commands. If your script waits for user input before clearing the screen, the Python input() tutorial is also useful.

Clear Python Shell in IDLE
IDLE is different from a normal terminal. The official IDLE documentation describes it as Python’s editor and shell, and its Shell menu includes Restart Shell. Restarting the shell resets the environment and display state, which is usually better than printing many blank lines.
Use this path in IDLE:
- Open the Shell menu.
- Choose Restart Shell.
- Confirm the restart if IDLE asks.
If you only print blank lines, the old output is still there when you scroll up. That can be acceptable for a quick classroom demo, but it is not a real reset.
Clear Screen vs Restart Python
Use clear screen when the output is messy but your variables should stay available. Restart Python when old variables, imports, or errors might affect your next test. Python’s interactive mode documentation explains the terminal prompt flow used by the Python interpreter.
| Goal | Best action |
|---|---|
| Hide old output | Clear the terminal screen |
| Remove variables and imports | Restart the interpreter |
| Fix PATH or launcher issues | Check the Python installation path |

If the Clear Command Does Not Work
If cls or clear does not work, first check where the code is running. A normal terminal, IDLE, a notebook, and an IDE output panel do not all behave the same way. Some IDE run panels show program output but do not emulate every terminal control feature.
- In a real terminal, run
clsorclearbefore starting Python. - In a Python script, use a fixed
os.system()command based on the operating system. - In IDLE, prefer Restart Shell when you need a clean environment.
- In an IDE, use the IDE’s own clear-output button if the terminal command is ignored.
Clear A Terminal Screen
On Windows Command Prompt or PowerShell, cls clears the visible console. On macOS and Linux terminals, clear does the equivalent. These commands do not delete objects from a Python process that is still running.
import os
command = "cls" if os.name == "nt" else "clear"
os.system(command)
Reset The Interpreter State
If old variables, imports, or side effects are the problem, clear the screen alone is insufficient. Exit and start Python again, restart the IDE console, or restart the notebook kernel when a clean process is required.
import sys
print("restart the process to reset imports and variables")
# sys.exit() ends the current interpreter when called from a scriptClear IDLE And Notebook Output
IDLE, Jupyter, VS Code, and other frontends own their output panes. Use their clear-output action rather than assuming a shell command will control a widget. In a notebook, clearing output does not necessarily reset kernel variables.
from IPython.display import clear_output
print("old output")
clear_output(wait=True)
print("new output")
Choose The Smallest Reset
Use a screen clear for readability, delete selected variables when only a namespace entry is stale, and restart the process when imports, threads, or global state must be rebuilt. Document the choice in reproducible examples so users do not confuse display cleanup with state cleanup.
name = "temporary"
print(name)
del name
print("selected variable removed")Python’s interpreter documentation explains the interactive process and exit behavior. Terminal commands and notebook display controls are frontend concerns, so use the control that matches where Python is running.
Shell Commands Are Frontend-Specific
A terminal command clears text in the terminal that interprets it; it does not send a universal instruction to every Python console. A notebook, IDE panel, and embedded terminal may capture output differently, so a portable utility should document its supported frontend instead of promising that one command clears every screen.
For reproducible debugging, prefer restarting the exact process or kernel when state matters. Record the operating system and frontend in support instructions, and avoid running a shell command from Python when the program should remain portable or when the command could be unsafe in the execution environment.
For related interactive workflows, compare exiting Python, current-directory inspection, and a terminal widget when the shell is embedded in another tool.
Frequently Asked Questions
How do I clear the Python shell on Windows?
Use the terminal’s cls command or restart the shell when the interpreter state also needs to be reset.
How do I clear the Python shell on macOS or Linux?
Use clear in the terminal, or restart the Python process when you need a fresh namespace and imported modules.
Does clearing the screen delete Python variables?
No. A screen-clear command changes visible terminal output; it does not remove variables from the running interpreter.
How do I clear output in Jupyter?
Use the notebook’s clear-output action or display functions appropriate to the frontend; restarting the kernel is a stronger state reset.