Skip to content

Installing and running Python

~15 min ยท prerequisite: What is a program

What you will be able to do

  • Check whether Python is already on your computer and which version it is.
  • Run a Python file from the terminal and see its output.
  • Start the interactive REPL, type a line of Python, and leave it.

The idea

To run Python code, you need two things: the Python interpreter installed on your computer, and a way to give it instructions. The second part is usually a terminal, a text window where you type commands. A terminal is also called a command line or shell. You type a command, press Enter, and the computer replies with text.

This course uses Python 3.12 or a newer 3.x release. The official downloads page is python.org/downloads. Download the installer for your system and follow its steps. Some computers already have Python, so check first.

To check, open a terminal and type the following command, then press Enter.

python3 --version

On macOS and Linux, use python3. On Windows, the launcher is usually called py, so try py --version. If the output starts with Python 3.12 or a higher version, you are ready. If the command is not found, install Python from the downloads page and try again.

Python can run in two ways. The first is the REPL, short for read, evaluate, print, loop. It is an interactive prompt that reads one line at a time and shows the result. It starts when you type python3 alone. The prompt looks like >>>. You type a line, press Enter, and Python answers. To leave, type exit() and press Enter.

The second way is a script, which is a file of Python code that you run all at once. Scripts end in .py. You create the file in any text editor, save it, and run it from the terminal with python3 filename.py. Scripts are what you will write for most of this course, because you can save them and run them again.

When you run a script, Python reads the file from the first line to the last. It does not print the result of every line the way the REPL does. Only the lines that ask for output, such as print, show something. You will learn print in lesson 3.

Keep the terminal open while you work. If you change the file, save it and run the command again. Each run starts fresh.

Worked example

Create a file named hello.py in an empty folder. Put one line in it.

print("Hello, world")

Line by line, this is what happens when you run it:

  1. The file contains one statement, print("Hello, world"). A statement is an instruction that Python carries out.
  2. print is a built-in function. A function is a named action you call with parentheses.
  3. The text inside the quotes is a string, a piece of text. Python sends it to the screen.

In the terminal, run this command from the same folder as the file.

python3 hello.py

The output is:

Hello, world

The REPL shows the same result if you type the line at the >>> prompt. The difference is that a script runs every line in the file, while the REPL waits for your next line.

Try it

The blocks below are checks that run in the browser. They do not need your computer's Python. Each assert must pass.

import sys

assert sys.version_info.major == 3

Next, a check of the basic idea that the interpreter evaluates an expression.

assert 2 + 3 == 5
assert "Hello, " + "world" == "Hello, world"

Common mistakes

  1. Running the command from the wrong folder. If your terminal is in a different folder from the file, Python cannot find it:

    python3: can't open file 'hello.py': [Errno 2] No such file or directory
    

    Use ls (macOS and Linux) or dir (Windows) to list the files in the current folder.

  2. Typing a command into the REPL. The REPL expects Python code. Typing the shell command python3 hello.py at the >>> prompt gives:

    SyntaxError: invalid syntax
    

    Exit the REPL with exit() and then run the command in the terminal.

  3. Using an old Python 2 style of print. Writing print "Hello" without parentheses gives:

    SyntaxError: Missing parentheses in call to 'print'. Did you mean print(...)?
    

    In Python 3, print is a function, so it needs parentheses.

  4. Typing a command that is not installed. On Windows, if Python is not on your path, you may see:

    'python3' is not recognized as an internal or external command,
    operable program or batch file.
    

    Try py instead, or reinstall Python and tick the option that adds it to your path.

Practice

  1. Find out which Python version your computer runs. Write the command you typed and the first line of the output in a comment in a file named version.py.
  2. Create a file named first.py that prints your name. Run it with python3 first.py.
  3. Open the REPL, type 2 * 21, and read the answer. Then type exit() to leave.
  4. Predict what this script prints, then run it to check: print("one") followed by print("two") on the next line.
  5. Stretch: create a script with two print lines, each showing a sum. Use one print for 7 + 8 and one for 10 * 3. Run it and check that the output is 15 and 30.
Solution
  1. Type python3 --version (or py --version on Windows). A typical output is Python 3.12.4. Your version number may differ.

  2. python print("Your Name")

Replace Your Name with your name. The terminal shows Your Name.

  1. The REPL shows 42. Type exit() to return to the terminal.

  2. The output is one on one line and two on the next line.

  3. python print(7 + 8) print(10 * 3)

The output is 15 on the first line and 30 on the second.

Recap

  • Check your Python version with python3 --version, or py --version on Windows.
  • A script is a .py file that you run from the terminal with python3 filename.py.
  • The REPL (>>>) runs one line at a time, and exit() leaves it.

Sources