Skip to content

Install and verify Python

  • Lesson 2
  • 30 min
  • Module 1: Computer and environment basics
Lesson 2 of 18 · Python for Practical AI EngineeringInstall and verify Python

What you will be able to do

Install Python, prove which Python your terminal runs, and run your first script. A computer can have more than one Python, and many beginner problems start when the wrong one runs.

Before you startWhat to know and to have readyAbout 30 minutes

What you need to know

What you need on your computer

  • The folder ~/projects/ticket-cleaner. You made it in Files and terminal: Guided practice. If it is missing, do that section again. It takes two minutes.
  • A terminal that you can move to that folder with cd ~/projects/ticket-cleaner, as in Your turn: move with paths.
  • Permission to install software on your computer.
  • About 200 MB of free disk space and an internet connection.

Tested with Python 3.14 on macOS, Windows (PowerShell) and Ubuntu 26.04. Any Python from 3.12 works for this course.

Python is a program that runs your program

Section titled “Python is a program that runs your program”

Python is a programming language, and it is also a program on your computer. That program is the interpreter. It reads a file of Python code and does what each line says, from the top to the bottom.

A Python program in a file is a script. The file name ends with .py. To run a script, you start the interpreter and give it the file name as an argument:

Terminal window
python3 first_run.py

A computer often has more than one interpreter. For example, a Mac can have an old Python from Apple and the new one that you install. The command that you type (python3, python or py) selects one of them. So after you install Python, you check two things: the version, and the location of the interpreter that runs.

Follow the steps for your system.

  1. Go to python.org/downloads.
  2. Select Download Python 3.14 (or the newest version that the page shows).
  3. Open the downloaded .pkg file and follow the installer. Keep the default options.
  4. When the installer ends, a Finder window opens with a Python 3.14 folder. Double-click Install Certificates.command. This lets Python check the identity of secure websites, which you need in Module 4.
  5. Close all terminal windows, then open a new terminal. A terminal finds new programs only when it starts.

Two commands answer the two questions: which version, and where is it.

  1. Show the version:

    Terminal window
    python3 --version
    Output
    Python 3.14.6

    Your numbers can be different. The first two numbers must be 3.12 or higher.

  2. Show the location of the interpreter. The -c option tells Python to run the code in the quotes instead of a file:

    Terminal window
    python3 -c "import sys; print(sys.executable)"
    Output
    /Library/Frameworks/Python.framework/Versions/3.14/bin/python3

    On macOS, the path must not be /usr/bin/python3. That is Apple’s old Python.

In the rest of this lesson, the commands use python3. On Windows, type python instead. From the next lesson, everyone types python.

A code editor is a program for writing code. It colours the code so it is easier to read, and it shows some mistakes before you run the code. This course uses Visual Studio Code (VS Code), which is free and works on all three systems. If you already use a different code editor, you can keep it.

  1. Download VS Code from code.visualstudio.com, and install it.
  2. Open VS Code. Select the Extensions icon on the left side (four small squares).
  3. Search for Python. Install the extension from Microsoft.
  4. Select File › Open Folder, and open your ticket-cleaner folder.
  5. Select Terminal › New Terminal. A terminal opens at the bottom of the window, already in the ticket-cleaner folder.

You can use the terminal in VS Code or a separate terminal window. They work the same.

Amira makes a script that reports which Python runs it. In VS Code, she selects File › New File, saves it as first_run.py in ticket-cleaner, and types this code:

first_run.py
import platform
import sys
print("ticket-cleaner: first run")
print("Python version:", platform.python_version())
print("Interpreter:", sys.executable)

Line by line:

Line What it does
import platform and import sys Load two parts of Python’s ready-made code. platform knows the Python version. sys knows where the interpreter is.
print(...) Shows text in the terminal. Each print makes one line of output.
"ticket-cleaner: first run" Text in quotes is shown exactly as you wrote it.
platform.python_version() Gets the version number, for example 3.14.6. print shows it after the text, with a space between them.

She runs it from the project folder:

Terminal
cd ~/projects/ticket-cleaner
python3 first_run.py
Output (macOS)
ticket-cleaner: first run
Python version: 3.14.6
Interpreter: /Library/Frameworks/Python.framework/Versions/3.14/bin/python3

Then she checks the exit code, as in the previous lesson:

Terminal window
echo $?
Output
0

The script got to the end without an error, so Python gave the exit code 0.

  1. In VS Code, make a new file. Save it as first_run.py in your ticket-cleaner folder.

  2. Type the code from the worked example. Save the file with Ctrl+S (Cmd+S on macOS).

  3. In the terminal, make sure that you are in the project folder:

    Terminal window
    pwd

    The output ends with ticket-cleaner.

  4. Run the script:

    Terminal window
    python3 first_run.py

    You see three lines. The version and the interpreter path are the same as in Step 2.

  5. Show the exit code. On macOS and Linux, run echo $?. In PowerShell, run $LASTEXITCODE.

    The output is 0.

Add one line to the end of first_run.py. It shows the working directory that the program sees:

first_run.py (add at the end)
import os
print("Working directory:", os.getcwd())

Run the script two times. Before each run, predict the last line of the output.

  1. From the project folder: python3 first_run.py.
  2. From the projects folder: run cd .., then python3 ticket-cleaner/first_run.py.
Show the answer

The first run shows .../projects/ticket-cleaner. The second run shows .../projects. The script file is in the same place both times, but the working directory is the folder that the terminal is in. When a later script opens data/tickets.csv, Python looks for it from the working directory. So you run the project’s scripts from the project folder.

Common mistake

Can't open file

Symptom: you run python3 first_run.py, and you see an error like this. The exit code is 2.

python3: can't open file '/Users/amira/first_run.py': [Errno 2] No such file or directory

Cause: Python looks for first_run.py in the working directory. The path in the message shows where it looked: here, the home folder, not ticket-cleaner. If the path is correct, the file name can be different, for example first-run.py or first_run.py.txt.

Fix:

  1. Run pwd. If you are not in ticket-cleaner, run cd ~/projects/ticket-cleaner.
  2. Run ls. Check that first_run.py is in the list, with exactly that name.
  3. Run the script again.

Common mistake

The wrong Python runs

Symptom: one of these:

  • On macOS, python3 --version shows Python 3.9.6, or sys.executable shows /usr/bin/python3.
  • On Windows, python opens the Microsoft Store, or shows Python was not found.
  • On any system, python3: command not found.

Cause: the terminal found a different Python, or no Python, in the folders on its PATH. This often happens when the terminal was open during the installation, so it does not know about the new program.

Fix:

  1. Close every terminal window, including the terminal in VS Code. Open a new one.
  2. Run the two commands from Step 2 again.
  3. If the problem continues on macOS, run the python.org installer again.
  4. If the problem continues on Windows, open the Start menu and search for Manage app execution aliases. Make sure that Python (default) and Python install manager are On. Then open a new terminal.

Check your understanding

Your terminal is in ~/projects. You run python3 ticket-cleaner/first_run.py. Does it work? What does the Working directory line show?

Show the answer

It works, because ticket-cleaner/first_run.py is a correct relative path from ~/projects. The last line shows the projects folder, because the working directory is the terminal’s folder, not the folder of the script.

Check your understanding

Practical check: show the version and the location of the Python that your terminal runs. How do you know that it is not an old system Python?

Show the answer

Run python3 --version (python --version on Windows) and python3 -c "import sys; print(sys.executable)". The version is 3.12 or newer. On macOS, the location is not /usr/bin/python3. On Windows, the location is in your user folder, under AppData\Local\Python.

Recap

  • The interpreter is the program that runs Python code. A script is a .py file that you run with python3 file.py.
  • A computer can have more than one Python. Check the version with --version and the location with sys.executable.
  • A script sees the working directory of the terminal, not the folder where the script is. Run project scripts from the project folder.
  • A script that ends without an error gives the exit code 0.

Next, you give the project its own Python environment, so the packages that it needs do not affect other projects.

Next: Isolate dependencies