Lesson 2 / 1118%

Lesson 2

Installing Python

15 min

Download Python, install it on your system and set up VS Code.

Definition

Installing Python means placing on your computer theinterpreter (the program that reads and runs your code), the standard library (ready-to-use modules) and the pip tool (the package manager). Once the installation is complete, thepythoncommand becomes available in any terminal, thanks to thePATH system variable, which tells your computer where to find it.

Downloading Python

Python can be downloaded for free from the official website,python.org, in the “Downloads” section. The site automatically detects your operating system and offers you the latest stable version of Python 3.

Always download Python from this official site and avoid third-party sites that offer “installers”: they may contain unwanted programs. Choose the most recent stable version, unless your school or your project requires a specific version. On a recent computer, the 64-bit version is the one you need.

The official Python download website

Installing on Windows

Run the downloaded.exefile. On the very first screen of the installer,be sure to check the “Add python.exe to PATH” box before clicking “Install Now”. This option lets you use Python from any terminal.

The installation takes one to two minutes. When it finishes, the installer displays “Setup was successful”: you can then close the window. If Windows asks for permission to make changes to your computer, accept.

The Python installation screen on Windows with the “Add python.exe to PATH” box checked

Installing on macOS and Linux

  • macOS: download the.pkgfile from python.org and follow the installation wizard. Just click “Continue” until the end.
  • Linux: Python is usually already installed. If not, use your distribution’s package manager (for examplesudo apt install python3on Ubuntu or Debian).

On Linux, it can also be useful to installpython3-pipto get pip, the package manager you will need later to add libraries.

Checking the installation

Open a terminal (or VS Code’s built-in terminal) and type the following command:

terminal
python --version

You should see a version number, for examplePython 3.12.4. On macOS and Linux, the command is often calledpython3:

terminal (macOS / Linux)
python3 --version

On Windows, ifpythondoesn’t respond, try the official launcherpy:

terminal (Windows)
py --version

Checking that pip works

pip is installed at the same time as Python. It is used to download libraries written by other developers. Check that it’s there with the following command:

terminal
pip --version

On macOS and Linux, the command may be calledpip3instead. You will see later how to install a package withpip install package_name. For now, it’s enough to know that the command exists.

Trying out Python in interactive mode

By simply typingpythonin the terminal, you open interactive mode: a space where you can write an instruction and immediately see its result. It’s ideal for quickly testing an idea.

interactive mode
>>> 2 + 3
5
>>> print("Python is installed!")
Python is installed!
>>> exit()

The>>>symbols indicate that Python is waiting for an instruction. To quit, typeexit()or use the shortcutCtrl + Zthen Enter on Windows,Ctrl + Don macOS and Linux.

Setting up VS Code for Python

If you use VS Code, install Microsoft’s official Pythonextension from the Extensions tab. It adds advanced syntax highlighting, auto-completion and a button to run your files with one click.

Once the extension is installed, open the command palette withCtrl + Shift + P, type “Python: Select Interpreter” and choose the version of Python you just installed. That way, VS Code will know which interpreter to use to run your programs.

Common problems

  • “python is not recognized”: Python’s folder is not in the PATH. Run the installer again and check the corresponding box.
  • Wrong version displayed: if you seePython 2.7, usepython3instead.
  • The terminal can’t see Python: close and reopen the terminal after the installation, because it needs to reload the PATH.

Good to know

If the terminal answers “python is not recognized”, it’s almost always because the “Add to PATH” box wasn’t checked. Run the installer again, choose “Modify”, then check the corresponding option.

Exercise

Install Python on your machine, open a terminal and check thatpython --version(orpython3 --version) does display a 3.x version. Then check thatpip --versionresponds too, then try interactive mode by calculating an addition. Finally, install the Python extension in VS Code and select your interpreter.