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
1. Computer and environment basics
2. Values and control flow
3. Functions and program structure
4. Files, data and external services
5. Debugging and reliability
6. From notebook to maintainable project
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 ready
What you need to know
- Paths, the working directory and exit codes, from Files and terminal.
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:
python3 first_run.pyA 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.
Step 1: install Python
Section titled “Step 1: install Python”Follow the steps for your system.
- Go to python.org/downloads.
- Select Download Python 3.14 (or the newest version that the page shows).
- Open the downloaded
.pkgfile and follow the installer. Keep the default options. - When the installer ends, a Finder window opens with a
Python 3.14folder. Double-click Install Certificates.command. This lets Python check the identity of secure websites, which you need in Module 4. - Close all terminal windows, then open a new terminal. A terminal finds new programs only when it starts.
On Windows, you install the Python install manager first. It installs Python, and it gives you the python and py commands.
-
Open the Microsoft Store app.
-
Search for
Python Install Manager, and select Get (or Install). If you cannot use the Store, download the install manager from python.org/downloads. -
Close all terminal windows, then open a new PowerShell terminal.
-
Install Python 3.14:
Terminal window py install 3.14 -
If the install manager asks to add a folder to your
PATH, typeyand press Enter.PATHis the list of folders where the terminal looks for commands. -
Close the terminal, then open a new one.
Most Linux systems include Python. First, check the version:
python3 --versionIf the version is 3.12 or newer, install only the extra parts that this course uses. If it is older, or the command is not found, install Python with the same command.
-
On Ubuntu or Debian, run:
Terminal window sudo apt updatesudo apt install python3 python3-venv python3-pipOn Fedora, run
sudo dnf install python3. -
Type your password when the terminal asks for it. The terminal shows nothing while you type the password. This is normal.
Step 2: check which Python runs
Section titled “Step 2: check which Python runs”Two commands answer the two questions: which version, and where is it.
-
Show the version:
Terminal window python3 --versionTerminal window python --versionTerminal window python3 --versionOutput Python 3.14.6Your numbers can be different. The first two numbers must be
3.12or higher. -
Show the location of the interpreter. The
-coption 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/python3Terminal window python -c "import sys; print(sys.executable)"Output C:\Users\amira\AppData\Local\Python\pythoncore-3.14-64\python.exeTerminal window python3 -c "import sys; print(sys.executable)"Output /usr/bin/python3On 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.
Step 3: get a code editor
Section titled “Step 3: get a code editor”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.
- Download VS Code from code.visualstudio.com, and install it.
- Open VS Code. Select the Extensions icon on the left side (four small squares).
- Search for
Python. Install the extension from Microsoft. - Select File › Open Folder, and open your
ticket-cleanerfolder. - Select Terminal › New Terminal. A terminal opens at the bottom of the window, already in the
ticket-cleanerfolder.
You can use the terminal in VS Code or a separate terminal window. They work the same.
Worked example: the first script
Section titled “Worked example: the first script”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:
import platformimport 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:
cd ~/projects/ticket-cleanerpython3 first_run.pyticket-cleaner: first runPython version: 3.14.6Interpreter: /Library/Frameworks/Python.framework/Versions/3.14/bin/python3Then she checks the exit code, as in the previous lesson:
echo $?$LASTEXITCODEecho $?0The script got to the end without an error, so Python gave the exit code 0.
Guided practice: run your first script
Section titled “Guided practice: run your first script”-
In VS Code, make a new file. Save it as
first_run.pyin yourticket-cleanerfolder. -
Type the code from the worked example. Save the file with Ctrl+S (Cmd+S on macOS).
-
In the terminal, make sure that you are in the project folder:
Terminal window pwdThe output ends with
ticket-cleaner. -
Run the script:
Terminal window python3 first_run.pyYou see three lines. The version and the interpreter path are the same as in Step 2.
-
Show the exit code. On macOS and Linux, run
echo $?. In PowerShell, run$LASTEXITCODE.The output is
0.
Your turn: where does the script run?
Section titled “Your turn: where does the script run?”Add one line to the end of first_run.py. It shows the working directory that the program sees:
import osprint("Working directory:", os.getcwd())Run the script two times. Before each run, predict the last line of the output.
- From the project folder:
python3 first_run.py. - From the
projectsfolder: runcd .., thenpython3 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 directoryCause: 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:
- Run
pwd. If you are not inticket-cleaner, runcd ~/projects/ticket-cleaner. - Run
ls. Check thatfirst_run.pyis in the list, with exactly that name. - Run the script again.
Common mistake
The wrong Python runs
Symptom: one of these:
- On macOS,
python3 --versionshowsPython 3.9.6, orsys.executableshows/usr/bin/python3. - On Windows,
pythonopens the Microsoft Store, or showsPython 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:
- Close every terminal window, including the terminal in VS Code. Open a new one.
- Run the two commands from Step 2 again.
- If the problem continues on macOS, run the python.org installer again.
- 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.
Does your answer have the main points of ours?
Sign in and enroll in the course to save your answers. Every check counts toward the certificate.
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.
Does your answer have the main points of ours?
Sign in and enroll in the course to save your answers. Every check counts toward the certificate.
Recap
- The interpreter is the program that runs Python code. A script is a
.pyfile that you run withpython3 file.py. - A computer can have more than one Python. Check the version with
--versionand the location withsys.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