Python
Topic 2 · Getting started

Set Up Python on Your Computer

Install Python on Windows, macOS or Linux, install VS Code, run your first .py file, use the terminal, install packages with pip inside a virtual environment, open Jupyter notebooks, and fix the problems almost everyone meets.

Show:
In this topic
Beginner

Do you need this yet?

Not to learn: every lesson runs in your browser. But every real project, job and university assignment runs Python on a computer, so you will need it sooner or later. It takes about 20–30 minutes. You can do it now, or come back after the Loops lesson.

You will install two things:

  • Python itself: the program that reads and runs your code (called the interpreter).
  • VS Code: a free editor, a program for writing code, like a word processor for programs. It colours your code, spots mistakes and has a Run button.

Using a Chromebook, a tablet or a locked work computer?

Use Google Colab ↗: free Python notebooks in the browser with a Google account, nothing to install. It is what many AI courses use, and it has free GPUs for deep learning later.

Beginner

Step 1: install Python

Choose your computer. Use Python 3.12 or newer; any recent version is fine. Do not use Python 2, which is obsolete.

  1. Open the Microsoft Store, search for Python install manager, and press Install. (It is also on python.org/downloads/windows ↗.) This is the method python.org recommends for Windows.
  2. Close any open terminal windows. Press the Windows key, type Terminal (or PowerShell), and open it.
  3. Type this and press Enter. The first time, it may ask a few setup questions: accept the suggested answers.
PS> py install default PS> py --version Python 3.14.6

If you see a version number, Python is installed. On Windows you start Python with py (and usually python works too).

Older guides tell you to download a .exe installer and tick "Add python.exe to PATH". That still works, but python.org is retiring that installer, so use the install manager for new setups.

  1. Go to python.org/downloads/macos ↗ and download the latest macOS 64-bit universal installer. Open it and click through.
  2. Open the Terminal app (press ⌘ + Space and type Terminal).
  3. Check it worked:
$ python3 --version Python 3.14.6

On a Mac, always type python3 and pip3, not python. The plain python command may not exist, or may be an old copy Apple uses for itself.

If you already use Homebrew, brew install python works too.

Most Linux systems already have Python 3. Open a terminal and check:

$ python3 --version Python 3.12.3

On Ubuntu or Debian, also install the tools for virtual environments and packages:

$ sudo apt update $ sudo apt install python3 python3-venv python3-pip

Use python3 and pip3 (inside a virtual environment, plain python and pip work). Never remove or replace the system Python: Linux uses it for itself.

Beginner

Step 2: install VS Code

  1. Download Visual Studio Code from code.visualstudio.com ↗ and install it (on Windows, tick "Add to PATH" if asked).
  2. Open VS Code. Click the Extensions icon on the left (four small squares), search for Python, and install the one published by Microsoft. It also installs Pylance, which spots mistakes as you type.

Other good editors exist (PyCharm, or Jupyter for notebooks), but VS Code is free, popular in industry and used in this course.

Beginner

Step 3: write and run your first file

  1. Make a folder for your work, for example python-course in your Documents.
  2. In VS Code: File → Open Folder… and choose it.
  3. File → New File…, name it hello.py. The .py ending tells everyone it is a Python file.
  4. Type this and save (Ctrl + S, or ⌘ + S on a Mac):
name = "Sita" print("Hello,", name) print("2 + 3 =", 2 + 3)
  1. Press the ▷ Run button at the top right. A terminal opens at the bottom and shows:
Hello, Sita 2 + 3 = 5

You can also run it yourself from the terminal. In VS Code open one with Terminal → New Terminal; it starts in your folder. Then:

PS> py hello.py Hello, Sita 2 + 3 = 5
$ python3 hello.py Hello, Sita 2 + 3 = 5
$ python3 hello.py Hello, Sita 2 + 3 = 5

VS Code shows "Select Interpreter"?

Press it (or press Ctrl + Shift + P and type Python: Select Interpreter) and choose the Python you installed. VS Code needs to know which Python to use.

Beginner

The terminal in six commands

The terminal (also called the command line, shell or console) is a window where you type commands instead of clicking. It looks old-fashioned but it is how professionals install packages, run programs and use git. You only need a few commands:

Do thisWindows (PowerShell)macOS / Linux
Where am I?pwdpwd
What is in this folder?ls (or dir)ls
Go into a foldercd python-coursecd python-course
Go up one foldercd ..cd ..
Make a foldermkdir project1mkdir project1
Run a Python filepy hello.pypython3 hello.py

Two tricks save a lot of typing: press Tab to complete a file or folder name, and press ↑ to bring back the previous command.

Beginner

The Python shell: try one line at a time

Type py (Windows) or python3 (macOS, Linux) on its own and you get the interactive shell, also called the REPL. The >>> prompt means "Python is waiting for a line". It runs each line as soon as you press Enter and shows the result, which is handy for quick experiments:

$ python3 >>> 2 ** 10 1024 >>> "data".upper() 'DATA' >>> exit()

Type exit() to leave. Do not try to run a file from inside the shell: python hello.py typed after >>> gives a SyntaxError, because that is a terminal command, not Python.

Beginner

Step 4: packages, pip and virtual environments

A package (or library) is code someone else wrote that you can reuse: pandas for tables, Matplotlib for charts, scikit-learn for machine learning. You install packages with pip, Python's package installer, which downloads them from the Python Package Index (PyPI).

Always install packages inside a virtual environment: a private folder of packages for one project. Why? Project A may need an old version of a package and project B a new one; with one environment per project they never clash, and you can delete an environment and rebuild it without breaking anything else. Professionals do this for every project.

In your project folder, in the terminal:

# 1. create the environment (once per project): a folder called .venv PS> py -m venv .venv # 2. switch it on (every time you open a new terminal) PS> .venv\Scripts\activate # the prompt now starts with (.venv) # 3. install packages into it (.venv) PS> python -m pip install pandas matplotlib # 4. switch it off when you are done (.venv) PS> deactivate

If PowerShell says running scripts is disabled, run Set-ExecutionPolicy -Scope CurrentUser RemoteSigned once, then activate again.

# 1. create the environment (once per project) $ python3 -m venv .venv # 2. switch it on (every new terminal) $ source .venv/bin/activate # 3. install packages into it (.venv) $ python -m pip install pandas matplotlib # 4. switch it off (.venv) $ deactivate
# 1. create the environment (once per project) $ python3 -m venv .venv # 2. switch it on (every new terminal) $ source .venv/bin/activate # 3. install packages into it (.venv) $ python -m pip install pandas matplotlib # 4. switch it off (.venv) $ deactivate

Inside an active environment, python and pip always mean "this project's Python", on every system. VS Code usually notices .venv and offers to use it; say yes (or pick it with Python: Select Interpreter).

To let someone else (or future you) rebuild the same environment, save the list of packages and reinstall from it:

(.venv) $ python -m pip freeze > requirements.txt (.venv) $ python -m pip install -r requirements.txt

Writing python -m pip instead of just pip guarantees pip installs into the same Python you run, which avoids the most common "I installed it but Python can't find it" problem.

Beginner

Step 5: Jupyter notebooks

A notebook (.ipynb file) mixes code, results, charts and notes in cells you run one at a time. Data scientists use notebooks to explore data, and scripts (.py files) for finished programs. Two easy ways to get them:

  • In VS Code: with your environment active, run python -m pip install ipykernel. Then create a file ending in .ipynb; VS Code opens it as a notebook. Choose your .venv as the kernel (top right).
  • In the browser: run python -m pip install notebook, then jupyter notebook. It opens a page in your browser.
  • No install at all: Google Colab ↗. The playground on this site can also export your code as a notebook (More → Export as Jupyter notebook) to open in Colab.

In a notebook, press Shift + Enter to run a cell. Cells can run in any order, which is convenient but can confuse you: if results look strange, use Restart and Run All.

Beginner

Fixing the usual problems

You seeWhat it meansFix
'python' is not recognized… (Windows)The terminal cannot find Python.Use py instead. If that fails too, install the Python install manager, then close and reopen the terminal.
Typing python opens the Microsoft StoreWindows' placeholder "app alias" answered instead of Python.Use py, or turn off the python.exe aliases in Settings → Apps → Advanced app settings → App execution aliases.
command not found: python (Mac, Linux)The command is called python3 there.Type python3 and pip3 (or activate a virtual environment).
ModuleNotFoundError: No module named 'pandas' even though you installed itYou installed it into a different Python than the one running your code.Activate the project's environment, run python -m pip install pandas, and select that environment in VS Code.
error: externally-managed-environmentYour system does not let pip change its own Python.Create and activate a virtual environment first (Step 4).
SyntaxError after typing python hello.pyYou typed it inside the Python shell (>>>).Type exit() first, then run the command in the terminal.
can't open file 'hello.py': No such file or directoryThe terminal is in a different folder.Use cd to go to the folder with the file (ls should list it), or open the folder in VS Code.
Beginner

Summary

Your checklist: Python installed (py --version or python3 --version works), VS Code with the Python extension, a project folder with a .venv virtual environment, and hello.py running. That is a professional setup, the same one used at work.

Key words

interpreter
the program that reads and runs Python code
editor
a program for writing code (VS Code)
terminal
a window where you type commands
script
a .py file you run from start to end
REPL / shell
the >>> prompt that runs one line at a time
package
reusable code written by others, installed with pip
pip
the tool that installs packages
virtual environment
a private set of packages for one project (.venv)
notebook
a file of code cells, outputs and notes (.ipynb)

Common mistakes

  • Installing packages without a virtual environment, then mixing up versions between projects.
  • Typing pip install and getting a different Python than the one running your code: use python -m pip install.
  • Running terminal commands inside the >>> shell, or Python code in the terminal.
  • Saving a file as hello.py.txt: make sure the name really ends in .py.
  • Naming your own file after a package, such as pandas.py or random.py. Python then imports your file instead of the real package.
Check yourself

Quiz

1. On Windows, which command starts Python in the terminal with the current official installer?

2. On a Mac, which command reliably runs the Python you installed?

3. Why use a virtual environment for each project?

4. You installed pandas but get ModuleNotFoundError. What is the most likely cause?

5. What does python -m pip freeze > requirements.txt do?

Ready