Sub topic 1
Starting the interactive interpreter
The Python interactive interpreter reads one statement at a time, runs it, and prints the result. It is also called the REPL, short for read-eval-print loop. This chapter covers how to start it, how to tell which Python you got, and what you see when it starts. It also covers a few startup options and the common problems that stop a session from opening or make it behave oddly.
Prerequisites
This chapter assumes you have the following:
- Basic Python syntax: variables, functions, loops, and conditionals. You will not write much code here, but the examples use simple expressions.
- Experience running commands in a terminal or command prompt, including changing directories.
- Python 3 installed and available as
pythonorpython3.
If you are unsure about the last point, the first section shows how to check.
Starting the shell
You start the interactive interpreter by running the Python executable with no script name. The command name depends on your platform.
| Platform | Typical command | Notes |
|---|---|---|
| Linux | `python3` | `python` may be missing, or on older systems may point to Python 2. |
| macOS | `python3` | Installed with developer tools or from python.org or Homebrew. |
| Windows | `py` or `python` | `py` is the Python launcher that python.org installers provide. |
On Linux or macOS, type this in a terminal:
python3On Windows, type this in Command Prompt or PowerShell:
pyPython prints a banner and then the primary prompt:
Python 3.12.3 (main, Aug 14 2025, 17:47:21) [GCC 13.3.0] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>>Your version, build date, and compiler text will differ. The banner tells you three useful things:
- The Python version, here 3.12.3.
- The build information in parentheses and brackets, which names the compiler and distinguishes builds such as Anaconda from system Python.
- The platform, here
linux.
The >>> is the primary prompt. It means Python is waiting for a new statement. Type an expression and press Enter:
>>> 6 * 7
42The shell evaluated the expression and echoed its value. The next chapters explain exactly how that echo works. For now, you only need to know the session is live.
Leaving the shell
You will need a way out before you try anything else. These all work:
- Call
exit()orquit(). - On Linux and macOS, press Ctrl-D on an empty line.
- On Windows, press Ctrl-Z and then Enter.
A later chapter covers the details, including why exit without parentheses prints a hint instead of leaving.
Checking which Python you started
Having several Pythons on one machine is common. A system Python, a Homebrew or python.org Python, and the Python inside a virtual environment can all exist at once. The banner shows the version, but not which installation you are in. Ask the interpreter itself:
>>> import sys
>>> sys.version_info[:3]
(3, 12, 3)
>>> sys.executable
'/usr/bin/python3'sys.version_info is a named tuple, so slicing it gives the first three fields: major, minor, and micro. sys.executable is the full path of the interpreter running your session. When a package seems to be missing even though you installed it, compare this path with the Python your package manager used. A mismatch is the most common cause.
You can run the same checks without entering the shell. These commands print and exit:
python3 --version
python3 -c "import sys; print(sys.executable)"The -c option runs the string that follows it as a program. Without -i, Python exits afterward instead of opening the prompt.
Choosing a start-up form
Python decides whether to open the interactive prompt from how you invoke it. The diagram shows the common cases.
One more rule: if standard input is not a terminal, Python does not behave interactively. For example, echo "print(1)" | python3 reads the program from the pipe, runs it, and prints no banner or prompts. The interactive shell needs a real terminal, called a TTY. You can check this inside a session:
>>> import sys
>>> sys.stdin.isatty()
TrueUseful startup options
These options change how the session starts. They go before any script name.
| Option | Effect |
|---|---|
| `-q` | Quiet start. Skips the version and copyright banner. |
| `-i` | After running a script or `-c` code, stay in the shell. |
| `-I` | Isolated mode. Ignores environment variables such as `PYTHONPATH` and the user site directory, and does not put the current directory on `sys.path`. |
| `-u` | Unbuffered output streams. Rarely needed in the shell itself. |
A quiet start is handy when you open the shell often:
python3 -q>>> The -i option is covered later in this tutorial, in the chapter on combining scripts with the shell. A one-line preview shows the idea:
python3 -i -c "x = 41">>> x + 1
42The code in -c ran first, and its variable x survived into the interactive session.
The working directory affects what you can import
When you start the shell, Python puts an empty string at the front of sys.path. That entry means "the current directory." Check it:
>>> import sys
>>> sys.path[0]
''Your working directory therefore decides which of your own .py files you can import. If you start the shell in a project folder that contains helpers.py, then import helpers works. If you start it from your home directory, it fails with ModuleNotFoundError.
This has a trap. Python looks in the current directory before it looks in the standard library. A file named random.py or math.py in your working directory will be imported instead of the standard module of the same name. The confusing symptoms are errors such as AttributeError: module 'random' has no attribute 'randint'. When a standard module behaves strangely, check its location:
>>> import random
>>> random.__file__
'/usr/lib/python3.12/random.py'If that path points into your own project, rename your file and delete any __pycache__ folder beside it.
Starting inside a virtual environment
A virtual environment is a folder that holds its own Python interpreter and installed packages. Starting the shell from inside one gives you that environment's packages, not the system's. Create and activate one on Linux or macOS like this:
python3 -m venv .venv
source .venv/bin/activate
pythonOn Windows PowerShell, the activation line is .venv\Scripts\Activate.ps1. After activation, python refers to the environment's interpreter, and sys.executable shows a path inside .venv. The -m option used in the first command runs a module as a program, here the standard venv module.
Without activation, you can still run the environment's interpreter directly by path, for example .venv/bin/python. That also gives you the environment's packages.
Newer interpreters and the shell's look
Starting with Python 3.13, the default shell has a rewritten line editor. It adds colored prompts and tracebacks, multi-line editing of a whole block, and special keys such as F2 for history browsing and F3 for paste mode. The core behavior is the same: >>> prompts, echoed results, and persistent session state. If the new shell causes trouble in your terminal, set the environment variable PYTHON_BASIC_REPL=1 before starting Python to get the older basic shell.
On earlier versions, line editing and history depend on the readline module. It is usually present on Linux and macOS, and Windows has its own console history. A later chapter covers editing and history in detail.
Common startup problems
| Symptom | Likely cause | What to try |
|---|---|---|
| `python: command not found` | The name is not on your PATH, or only `python3` exists. | Try `python3`. On Windows, try `py`. |
| Banner says Python 2.7 | `python` points to an old interpreter. | Use `python3`, and check with `--version`. |
| Windows opens the Microsoft Store | An app execution alias stands in for a missing install. | Install Python, or disable the alias in Windows settings. |
| `ModuleNotFoundError` for your own file | The shell started in a different directory. | `cd` to the project folder, then restart Python. |
| Installed package is not found | The shell uses a different interpreter than your installer. | Compare `sys.executable` with the interpreter your installer used. |
| Arrow keys print `^[[A` | The interpreter was built without `readline`. | Use a Python from python.org or your package manager. |
Summary of what a good start looks like
A healthy session start has these properties. The banner version is the one you expect. sys.executable points at the interpreter you intend to use, such as one inside your virtual environment. Your working directory is the project folder, so your own modules import. With those three checks done in a few seconds, later problems are easier to diagnose.
Exercise
You start python3 in a folder that contains a file named math.py you wrote. Then you run import math and math.sqrt(16), and get an AttributeError. What is the cause, how can you confirm it from inside the shell, and what is the fix?
Show answer
Python puts the current directory first on sys.path (shown as ''), so import math loads your own math.py instead of the standard library module. Confirm it with math.__file__, which shows a path in your project folder. Fix it by renaming your file, deleting its __pycache__ copy, and restarting the shell.
Exercise
A package you installed with pip cannot be imported in your shell. Which two checks from this chapter help you find out whether the shell and pip use different interpreters, and what command installs the package into the right one?
Show answer
Run import sys; sys.executable in the shell, and compare it with the interpreter your pip belongs to, for example by running python3 -m pip --version. If they differ, install with python3 -m pip install name using the same interpreter you used to start the shell.