KAHIBARO
Discord Login Register

5.2 Installing Python

Why Installing Python Matters

Before you can build any backend in Python, you need a working Python interpreter on your machine. Installing Python is not just about downloading a file. You also want:

This chapter walks you step by step through installing Python on Windows, macOS, and Linux, and verifying that the installation works.

Always use Python 3 for new backend projects.
Python 2 is end-of-life and must not be used for new development.

We will focus on Python 3.11+ because many modern frameworks and libraries expect recent Python versions.


Checking if Python Is Already Installed

Before installing, check whether you already have Python and which version it is.

Windows

  1. Open Command Prompt:
    • Press Win key, type cmd, press Enter.
  2. Run:
bash
   python --version

or

bash
   py --version

Possible outcomes:

Output exampleMeaning
Python 3.11.5You already have Python 3.11.5 installed.
Python 2.7.18Old Python 2, you still need Python 3.
'python' is not recognized...No python on PATH, or not installed.

On Windows, the py launcher is often available even when python is not. It can manage multiple Python versions. For example:

bash
py -0

lists installed versions.

macOS

  1. Open Terminal:
    • Applications → Utilities → Terminal, or search "Terminal" in Spotlight.
  2. Run:
bash
   python3 --version

If you see something like Python 3.11.5, you are good. If you get command not found, Python 3 is not installed, or not on your PATH.

macOS might have an old python command that is Python 2 or a system stub. Always use python3, not python.

Linux

  1. Open your terminal.
  2. Try:
bash
   python3 --version

Most modern distributions ship with Python 3. You might see Python 3.10.12 or similar. That is usually fine for starting out.

If you see command not found, you will need to install Python using your distro package manager or another method.


Choosing a Python Version

For backend work, aim for:

Why this matters:

You can always check the official site:

Look for the latest Python 3.x.x release with a green "Latest Python 3 Release" label.

Rule of thumb:
Use the latest stable Python 3 unless you have a specific reason not to. Never start a new backend with Python 2.


Installing Python on Windows

Step 1: Download the Installer

  1. Open your browser.
  2. Go to https://www.python.org/downloads/windows/
  3. Under "Stable Releases", find the latest Python 3.x.x.
  4. Click "Download Windows installer (64-bit)".
    • If you are unsure, 64-bit is almost always correct nowadays.

This will download a file like python-3.11.5-amd64.exe.

Step 2: Run the Installer with Correct Options

Double-click the downloaded .exe file. The first screen is important.

  1. At the bottom of the first window, check:
    • Add python.exe to PATH

This lets you type python in Command Prompt from any folder.

  1. Click "Customize installation" instead of "Install Now". This gives you more control.
  2. On the next screen, leave default options checked:
    • Documentation
    • pip
    • tcl/tk and IDLE
    • Python test suite
    • py launcher
    • for all users (optional, see below)
  3. Click Next.

On the "Advanced Options" page:

Click Install and wait for the installer to finish.

Step 3: Verify the Installation

Close and reopen Command Prompt, then run:

bash
python --version

or

bash
py --version

You should see something like:

text
Python 3.11.5

Try running Python interactively:

bash
python

You should see:

text
Python 3.11.5 (tags/...)
>>>

Type:

python
print("Hello from Python")

Then press Enter. You should see:

text
Hello from Python

Exit Python with:

python
exit()

or press Ctrl+Z then Enter.

If python does not work but py does, you can use:

bash
py -3

to start Python 3, and:

bash
py -3.11

to start a specific version.


Installing Python on macOS

On macOS the simplest way for beginners is to use the official installer. More advanced users often prefer homebrew, but both options are fine.

Option 1: Official Python Installer

Step 1: Download

  1. Go to https://www.python.org/downloads/macos/
  2. Download the latest "macOS 64-bit universal2 installer" for Python 3.x.x. The file will be something like python-3.11.5-macos11.pkg.

Step 2: Install

  1. Double-click the .pkg file in your Downloads folder.
  2. Follow the installer steps, accept the license, and keep default installation options.
  3. At the end it should say installation was successful.

Step 3: Verify

Open Terminal and run:

bash
python3 --version

Expect:

text
Python 3.11.5

Run Python:

bash
python3

You should see the Python prompt:

text
>>>

Test it:

python
print("Hello from Python on macOS")

Exit with:

python
exit()

Option 2: Homebrew (for users comfortable with the command line)

If you already use Homebrew or want to, you can install Python like this.

Step 1: Install Homebrew (if not installed)

In Terminal:

bash
/bin/bash -c "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/HEAD/install.sh)"

Follow on-screen instructions. You may need to add Homebrew to your PATH. The installer usually prints the exact commands you need to run.

Step 2: Install Python

bash
brew install python

This will install the latest Python 3 (usually aliased as python3 and pip3).

Verify:

bash
python3 --version

Installing Python on Linux

On Linux, there are two main approaches:

For beginners, the package manager is usually enough.

Using the Distribution Package Manager

Ubuntu / Debian

Update package lists:

bash
sudo apt update

Install Python and pip:

bash
sudo apt install -y python3 python3-pip

Check:

bash
python3 --version
pip3 --version

Fedora

bash
sudo dnf install -y python3 python3-pip

Check:

bash
python3 --version

CentOS / RHEL (modern versions)

For newer releases:

bash
sudo dnf install -y python3 python3-pip

For older releases, you may need EPEL or a Software Collection, but for backend beginners, start with a modern distro when possible.

Arch Linux

bash
sudo pacman -S python python-pip

Check:

bash
python --version

On Arch, python is usually Python 3.

Using pyenv (optional, more advanced)

pyenv lets you install multiple Python versions side by side. This is useful when:

Basic steps (example for Ubuntu):

bash
# Install dependencies
sudo apt update
sudo apt install -y build-essential curl git \
  libssl-dev zlib1g-dev libbz2-dev libreadline-dev \
  libsqlite3-dev wget llvm libncursesw5-dev xz-utils \
  tk-dev libxml2-dev libxmlsec1-dev libffi-dev liblzma-dev
# Install pyenv
curl https://pyenv.run | bash

Then follow the instructions shown at the end to add pyenv to your shell configuration (for example .bashrc or .zshrc).

Install a Python version:

bash
pyenv install 3.11.5
pyenv global 3.11.5

Check:

bash
python --version

Now python uses the version managed by pyenv.


Setting Up PATH and Launching Python

The PATH is a list of directories where your operating system looks for executable programs. For Python:

Verifying PATH on Each OS

Windows

In Command Prompt:

bash
where python

If Python is correctly on PATH, you will see something like:

text
C:\Users\YourName\AppData\Local\Programs\Python\Python311\python.exe

If you see "INFO: Could not find files" then it is not on PATH.

macOS / Linux

In Terminal:

bash
which python3

or

bash
which python

If installed correctly, you will see a path such as /usr/local/bin/python3 or something similar.

If you see nothing, Python is not on PATH.

Fixing PATH on Windows (if needed)

If you forgot to check "Add python.exe to PATH" in the installer, you can:

  1. Re-run the Python installer and make sure to select "Modify" or "Change", then enable "Add python.exe to PATH".
  2. Or, manually add the Python install directory to PATH via:
    • Start -> search "Environment Variables" -> "Edit the system environment variables".
    • Click "Environment Variables".
    • Under "User variables" select Path and click "Edit".
    • Click "New" and add the folder where python.exe is installed, for example:
      • C:\Users\YourName\AppData\Local\Programs\Python\Python311\
    • Click OK several times to close all dialogs.

Open a new Command Prompt and test:

bash
python --version

Fixing PATH on macOS / Linux (if needed)

If Python is installed but not found, you can append its directory to PATH in your shell configuration.

For example, suppose Python is in /usr/local/bin and you use Bash. Edit ~/.bashrc or ~/.bash_profile and add:

bash
export PATH="/usr/local/bin:$PATH"

Then reload:

bash
source ~/.bashrc

Now:

bash
python3 --version

should work.


Using Python Interactively and With Files

Once installed, you will use Python in two main ways:

  1. Interactive mode, where you type commands at the >>> prompt.
  2. Script mode, where you run a .py file.

Interactive Mode

Useful for quick tests and learning.

Example on any OS:

bash
python
# or on macOS/Linux
python3

At the prompt:

python
2 + 3

You should see:

text
5

You can also import modules:

python
import math
math.sqrt(16)

Result:

text
4.0

Exit with exit() or Ctrl+D on macOS/Linux, Ctrl+Z followed by Enter on Windows.

Running a Python Script

Create a file hello.py with the following content:

python
print("Hello from a Python script!")

Navigate to the folder containing hello.py in your terminal, then run:

bash
  python hello.py
bash
  python3 hello.py

You should see:

text
Hello from a Python script!

Common Installation Problems and Fixes

Even with simple installers, problems can happen. Here are some typical ones and how to resolve them.

Problem 1: Wrong Python Version Used

Symptom:

bash
python --version

shows Python 2.7.x, but you installed Python 3.

Solutions:

bash
  python3 --version
bash
  py -3 --version

If you want python to always mean Python 3, you may need to adjust PATH or update your system alternatives on Linux. For beginners, simply using python3 is enough.

Problem 2: `python` or `python3` Not Found

Symptom:

text
'python' is not recognized as an internal or external command

or:

text
command not found: python3

Causes:

Fix:

Problem 3: Multiple Python Installations

You might have:

Symptoms:

Tips:

bash
  which python
  which python3

On Windows:

bash
where python
where py

Problem 4: Permission Denied on Linux/Mac When Installing Packages

You might see something like:

text
PermissionError: [Errno 13] Permission denied: '/usr/local/lib/python3.11/...'

This often happens when trying to install packages globally without sudo, or when mixing system Python with user packages.

Even though this chapter does not cover pip in depth, the main idea is:

We will cover virtual environments and package management later, which is the proper solution.


Summary

By the end of this chapter you should be able to:

These are the foundations you need before you can work with virtual environments, packages, and eventually build backend applications with frameworks like FastAPI.

Views: 7

Comments

Please login to add a comment.

Don't have an account? Register now!