Sub topic 1
Starting the Interactive Interpreter
The Python interactive shell, also called the REPL (read-eval-print loop), reads one statement, runs it, prints the result, and waits for the next one. This chapter covers how to start it, how to confirm which Python you are talking to, and how to read what appears on screen. Later chapters cover leaving the shell, echoing, and multi-line input in detail.
Prerequisites
This chapter assumes you have:
- Basic Python syntax: variables, functions, loops, and conditionals.
- Some experience running commands in a terminal or command prompt.
- Python 3 installed and available as
pythonorpython3.
Launching the shell
Open a terminal and run the interpreter with no arguments. With no script to run, Python starts an interactive session.
python3The command name depends on your platform:
| Platform | Typical command | Notes |
|---|---|---|
| Linux | `python3` | `python` may be missing or point to another version |
| macOS | `python3` | Installed with developer tools or from python.org |
| Windows | `py` or `python` | `py` is the launcher that ships with the python.org installer |
If the command is not found, check your installation and your PATH before going further. You can verify the version without starting a session:
python3 --versionReading the banner and prompt
A successful start prints a banner and then a prompt. Your version and build details will differ.
Python 3.12.4 (main, Jun 7 2024, 06:33:07) [GCC 13.2.0] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>>The first line gives the version, the build date, the compiler, and the platform. The >>> is the primary prompt. It means Python is ready for a new statement. Type a line and press Enter:
>>> 2 + 3
5Do not type the >>> yourself. In this tutorial, lines starting with >>> are what you type, and lines without it are what Python prints.
Confirming which Python you started
Machines often have several Python installations, and an activated virtual environment changes which one python3 means. Ask the session itself:
>>> import sys
>>> sys.version
'3.12.4 (main, Jun 7 2024, 06:33:07) [GCC 13.2.0]'
>>> sys.executable
'/home/you/project/.venv/bin/python3'sys.executable is the path to the interpreter running your session. If it is not the one you expected, you probably started the shell before activating your virtual environment. Packages installed in one interpreter are not visible from another, so check this first when an import fails unexpectedly.
Useful startup options
Options go before any script name. Two matter for this chapter:
-qhides the banner, which is handy once you know your version.-iruns a script and then drops you into the shell. It is covered in a later chapter.
python3 -q>>> 10 // 3
3Version differences you may notice
Since Python 3.13, the default shell has a newer interface. It supports colored output and multi-line editing, and it shows help on F1. Older versions use a simpler prompt. The behavior you learn here, including prompts, echoing, and session state, is the same in both.
Common startup problems
- "command not found" or "'python' is not recognized": Python is not on your
PATH, or you used the wrong name for your platform. Trypython3orpy. - Wrong version in the banner: Another installation comes first on your
PATH. Runsys.executableto find out which one started. - A prompt other than
>>>: A...prompt means Python is waiting for the rest of a block. A plain shell prompt means you are not in Python at all.
Exercise
You activated a virtual environment, started python3, and import requests fails even though you installed requests in that environment. Which two lines typed at the prompt help you find out whether the shell is using the environment's interpreter?
Show answer
Type import sys and then sys.executable. If the printed path is not inside your virtual environment's directory, the shell started a different interpreter, for example because it was launched before activation or from a different terminal.