Python · Beginner
How to install Python and run your first script
Why it matters
Knowing how to install Python properly sounds too basic to matter, yet "my code works in the notebook but not in the terminal" and "pip installed it but Python can't find it" are the two most common problems freshers bring to their first team. Python is listed in 60% of the Data Analyst, 78% of the Data Engineer, 84% of the Data Scientist and 88% of the AI/ML Engineer job descriptions we read, and every one of those jobs assumes you can set up a machine and run a script without help.
This lesson gets you from nothing to a working setup, and teaches you the three questions that solve most setup problems: which Python am I running, from which folder, and where is my file?
The concept
Which version to install
Install a current Python 3 release: 3.13 is what this course uses, and anything from 3.12 upward works for every lesson. Avoid two things:
Very new releases in their first weeks. Libraries such as pandas or PyTorch can take a while to publish packages for a brand new version.
Python 2. It reached end of life on 1 January 2020. You will still meet Python 2 code in old blog posts and Stack Overflow answers (more on spotting it below).
Installing on each operating system
Windows
Download the Windows installer from python.org/downloads.
On the first screen, tick "Add python.exe to PATH". It is unticked by default, and forgetting it is the number one reason
python"is not recognized" afterwards.Keep the py launcher option ticked, then click Install Now.
Open a new PowerShell window (old windows keep the old PATH) and check:
python --version
py --list # every Python the launcher knows about
py -3.13 --version # run a specific version
where.exe python # which python.exe comes first on PATHIf python opens the Microsoft Store instead, Windows is using its "App execution aliases" stub. Turn off the two python.exe / python3.exe aliases under Settings, Apps, Advanced app settings, App execution aliases. The Store build of Python itself works for simple scripts, but it runs in a sandbox with redirected file and package folders, which confuses tools and tutorials. Use the python.org installer.
Newer releases also offer a "Python install manager" for Windows that installs and switches versions with commands like py install 3.13. Either route gives you the same py and python commands.
macOS
Download the macOS installer from python.org/downloads, or use Homebrew:
brew install python@3.13.Open a new Terminal window and check:
python3 --version
which python3macOS ships a /usr/bin/python3 that belongs to Apple's developer tools. Leave it alone: do not upgrade it or install packages into it. On macOS the command is python3, not python, unless you are inside a virtual environment.
Linux
Most distributions already have Python 3. Ubuntu 24.04 ships 3.12, which is fine for this course.
python3 --version
sudo apt install python3 python3-venv python3-pip # Debian and UbuntuThe system Python belongs to the operating system: package managers and system tools depend on it. Recent Debian and Ubuntu releases refuse pip install into it with an externally-managed-environment error. That error is protecting you. Install packages inside a virtual environment instead; the virtual environments lesson later in this module shows how.
python, python3 and py
| command | Windows (python.org install) | macOS | Linux |
|---|---|---|---|
python | Python 3 | usually missing | missing or Python 3 on newer distros |
python3 | often a Store stub | Python 3 | Python 3 |
py | the launcher, picks the newest Python 3 | not available | not available |
Inside an activated virtual environment, python always means that environment's Python on every system. That is one more reason to use one per project.
Checking what you are running
Three commands answer "which Python is this?", and you should run them whenever something behaves oddly:
python --version # the version
which python # macOS/Linux: the file the shell will run
python -c "import sys; print(sys.executable)" # the interpreter Python itself reportsOn Windows use where.exe python instead of which. If sys.executable is not the Python you expected, every package you install is going somewhere you are not looking.
REPL, script or notebook
| tool | what it is | use it for |
|---|---|---|
REPL (python with no file) | type a line, see the result | checking one expression, help(str.split) |
script (python report.py) | a .py file run top to bottom | anything you will run twice, schedule or share |
| Jupyter notebook | cells of code and output in a browser or VS Code | exploring data, charts, teaching |
The REPL looks like this, and prints the value of each expression for you:
Python 3.13.14 (main, Jun 11 2026, 12:30:59) [GCC 13.3.0] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> 2 + 3
5
>>> len("namaste")
7Notebooks are great for exploring, but they hide state: cells run out of order, and a variable you deleted from the code can still live in memory. Anything that goes to a colleague, a scheduler or production should end up as a script.
VS Code setup
Install VS Code and the Python extension published by Microsoft (it brings Pylance and the debugger with it).
Open your project folder (File, Open Folder), not a single file. VS Code uses the folder as the workspace and as the terminal's starting directory.
Press
Ctrl+Shift+P(Cmd+Shift+Pon macOS), run Python: Select Interpreter, and pick the Python you just installed (later: the project's.venv). The chosen interpreter shows in the status bar at the bottom right.Open the built-in terminal with
Ctrl+`and run scripts there, so you see exactly the command that ran.
Working directory and relative paths
When you write open("data/penguins.csv"), Python looks for data/penguins.csv relative to the current working directory: the folder your terminal is in when you run the command. It is not the folder the script lives in. That one fact causes a large share of beginner FileNotFoundErrors, and it is the core of the worked example below.
Free cloud options for low-spec or shared laptops
Google Colab: a free Jupyter notebook in the browser with Python and the common data libraries preinstalled. Run
!python --versionin a cell to see the version. Files you upload vanish when the session ends unless you save them to Google Drive.GitHub Codespaces: a full VS Code and Linux machine in the browser, built from a GitHub repository. Personal accounts get a monthly free quota of core-hours, and the GitHub Student Developer Pack adds more. Stop the codespace when you finish, because idle time counts.
Both are fine for learning. You still want a local install eventually, because interview take-home tasks and real jobs assume one.
Spotting Python 2 code online
Python 2 code fails on Python 3 with confusing errors. The giveaways:
| Python 2 | Python 3 |
|---|---|
print "hi" | print("hi") |
xrange(10) | range(10) |
raw_input() | input() |
dict.iteritems() | dict.items() |
7 / 2 gives 3 | 7 / 2 gives 3.5 (use // for 3) |
Python 3 tells you when you have pasted old code:
File "<string>", line 1
print "hi"
^^^^^^^^^^
SyntaxError: Missing parentheses in call to 'print'. Did you mean print(...)?Traceback (most recent call last):
File "<string>", line 1, in <module>
for i in xrange(3): pass
^^^^^^
NameError: name 'xrange' is not defined. Did you mean: 'range'?If a tutorial is full of these, find a newer one.
Worked example: a script that knows where it is
You will create a small project, run it from two different folders, watch it break, read the traceback, and fix it properly. The data file is the Palmer Penguins dataset (CC0), 344 penguins measured at Palmer Station, Antarctica. Download penguins.csv from the palmerpenguins repository (click "Download raw file").
Make this layout. On Windows your home folder is C:\Users\<you>; the outputs below come from a Linux machine where it is /home/asha.
first-project/
hello.py
data/
penguins.csvVersion 1: works on my machine
import os
import sys
print("Python version:", sys.version.split()[0])
print("Interpreter: ", sys.executable)
print("Working folder:", os.getcwd())
with open("data/penguins.csv", encoding="utf-8") as f:
rows = f.readlines()
print("Rows in file: ", len(rows) - 1)Run it from inside the project folder:
cd ~/first-project
python hello.pyPython version: 3.13.14
Interpreter: /usr/local/bin/python
Working folder: /home/asha/first-project
Rows in file: 344344 rows after subtracting the header line, which matches the dataset. Now run the same file from your home folder, the way a scheduler, a teammate or VS Code's Run button with a different workspace might:
cd ~
python first-project/hello.pyPython version: 3.13.14
Interpreter: /usr/local/bin/python
Working folder: /home/asha
Traceback (most recent call last):
File "/home/asha/first-project/hello.py", line 8, in <module>
with open("data/penguins.csv", encoding="utf-8") as f:
~~~~^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
FileNotFoundError: [Errno 2] No such file or directory: 'data/penguins.csv'Reading the traceback from the bottom up
A traceback reads top to bottom in time, so the useful part is at the bottom:
Last line: the exception type and message.
FileNotFoundError: ... 'data/penguins.csv'. Python looked for that exact relative path.The line above it: the code that failed, with
^^^^markers (Python 3.11 and newer) pointing at the failing call.The
File ... line 8line: which file and line. In bigger programs there are several of these frames; the bottom one is where the error was raised, and the lowest one in your code is usually where to look.
Put that together with the Working folder: /home/asha line printed just before, and the cause is clear: Python looked for /home/asha/data/penguins.csv, which does not exist.
Version 2: build paths from the script's own location
The fix is not "always cd first". The fix is to build the path from the location of the script file, which Python gives you as __file__:
import os
import sys
from pathlib import Path
HERE = Path(__file__).resolve().parent # the folder this file lives in
DATA = HERE / "data" / "penguins.csv"
print("Python version:", sys.version.split()[0])
print("Interpreter: ", sys.executable)
print("Working folder:", os.getcwd())
print("Data file: ", DATA)
with open(DATA, encoding="utf-8") as f:
rows = f.readlines()
print("Rows in file: ", len(rows) - 1)Run it from somewhere completely unrelated:
cd /tmp
python ~/first-project/hello.pyPython version: 3.13.14
Interpreter: /usr/local/bin/python
Working folder: /tmp
Data file: /home/asha/first-project/data/penguins.csv
Rows in file: 344The working folder changed and the script still found its file. Path joins with / and produces the right separator on Windows too, so the same code works on every system.
:::callout{variant=tip title="Which kind of path should you use?"} Files that ship with the script (lookup tables, templates, sample data): build from Path(__file__).resolve().parent. Files the user chooses (an input CSV, an output folder): take them as command-line arguments and resolve them against the working directory, which is what the user expects when they type a relative path. :::
Common mistakes
Not ticking "Add python.exe to PATH" on Windows.
pythonis then "not recognized", or it opens the Microsoft Store. Re-run the installer, choose Modify, and enable it, or usepywhich works regardless.Two Pythons, one confused pip. You install a package with
pipand getModuleNotFoundErrorwhen runningpython.pipandpythonpoint at different installs. Usepython -m pip install ...so pip always belongs to the Python you run, and checksys.executable.Installing packages into the macOS or Linux system Python with
sudo pip. It can break operating system tools. Use a virtual environment per project.Reading files with a path relative to the script. Relative paths resolve against the working directory. Use
Path(__file__).resolve().parentfor files next to your code.Opening a single file in VS Code instead of the folder. The interpreter selection, the terminal's start folder and the debugger all depend on the workspace folder.
Leaving an old terminal open after installing. The PATH change only reaches new terminals. Close it and open a fresh one before deciding the install failed.
Copying Python 2 code.
print "x"andxrangeare signs the tutorial is over a decade old.
Interview angle
Setup questions show up in screening calls and in the first minutes of a take-home review.
:::toggle{title="Which Python version do you use, and how do you set up a new project?"} "Python 3.13, installed from python.org (or Homebrew on a Mac). For each project I create a folder, a virtual environment with python -m venv .venv, select it as the interpreter in VS Code, and pin dependencies in a requirements file or lockfile. I check sys.executable if anything looks off." Naming the version, the editor and a per-project environment is what the interviewer is listening for. :::
:::toggle{title="Your script works when you run it but fails when the scheduler runs it with FileNotFoundError. Why?"} The scheduler starts the script from a different working directory, so relative paths point somewhere else. Build paths to bundled files from Path(__file__).resolve().parent, and pass user files as absolute paths or arguments. Also check the scheduler uses the same interpreter (the full path to the venv's Python). :::
:::toggle{title="What is the difference between a notebook and a script?"} A notebook is for exploration: cells, inline output, charts. Its state depends on the order you ran cells in, so it is hard to reproduce or schedule. A script runs top to bottom the same way every time, can take arguments, can be tested and scheduled. Explore in a notebook, move the logic into scripts and modules once it works. :::
Practice task
Install Python 3.13 (or any 3.12+) and VS Code with the Python extension. In a new terminal, record the output of
python --version(orpython3 --version) and the interpreter path fromwhich pythonorwhere.exe python.Create
first-project/withhello.py(version 1 above) anddata/penguins.csv. Run it from inside the folder and confirm it printsRows in file: 344.Run it from the parent folder and from your home folder. Copy the traceback and write one sentence that names the exception, the line and the cause.
Fix it with
Path(__file__).resolve().parentand prove it works from three different folders.Add one more line that prints how many rows mention
Gentoo.
Done when: the script prints the same row count no matter which folder you run it from, and you can explain why version 1 failed.
:::toggle{title="Show a solution for step 5"}
gentoo = sum(1 for line in rows if line.startswith("Gentoo,"))
print("Gentoo rows: ", gentoo)In penguins.csv the species is the first column, so checking the start of each line is enough; it prints 124. Matching on the column instead of anywhere in the line is a habit worth starting now: the CSV lesson later in this module shows how csv.DictReader does it properly. :::
Further reading
Using Python on Windows: the official guide to the installer, the PATH option and the
pylauncher.Using Python on macOS: what the python.org installer puts where.
Python in Visual Studio Code: the official setup walk-through, including interpreter selection.
pathlib documentation: every path operation you will need.