5.2 Installing Python
Table of Contents
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:
- The correct version (usually Python 3, not Python 2).
- A clean setup that works from the command line.
- A setup that does not conflict with your operating system Python.
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
- Open Command Prompt:
- Press
Winkey, typecmd, press Enter. - Run:
python --versionor
py --versionPossible outcomes:
| Output example | Meaning |
|---|---|
Python 3.11.5 | You already have Python 3.11.5 installed. |
Python 2.7.18 | Old 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:
py -0lists installed versions.
macOS
- Open Terminal:
- Applications → Utilities → Terminal, or search "Terminal" in Spotlight.
- Run:
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
- Open your terminal.
- Try:
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:
- A supported Python 3 version (3.9 or newer).
- Ideally the latest stable Python 3 (for example 3.11 or 3.12).
Why this matters:
- Modern frameworks such as FastAPI often require recent versions.
- Security updates and performance improvements exist only for supported versions.
- Very old versions may not run newer libraries.
You can always check the official site:
- https://www.python.org/downloads/
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
- Open your browser.
- Go to https://www.python.org/downloads/windows/
- Under "Stable Releases", find the latest Python 3.x.x.
- 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.
- At the bottom of the first window, check:
Add python.exe to PATH
This lets you type python in Command Prompt from any folder.
- Click "Customize installation" instead of "Install Now". This gives you more control.
- On the next screen, leave default options checked:
Documentationpiptcl/tk and IDLEPython test suitepy launcherfor all users(optional, see below)- Click Next.
On the "Advanced Options" page:
- Recommended settings:
- Check
Install for all usersif you have admin rights and want Python available for all accounts. - Check
Precompile standard library. - Leave install location as default (like
C:\Program Files\Python311\) unless you know what you are doing.
Click Install and wait for the installer to finish.
Step 3: Verify the Installation
Close and reopen Command Prompt, then run:
python --versionor
py --versionYou should see something like:
Python 3.11.5Try running Python interactively:
pythonYou should see:
Python 3.11.5 (tags/...)
>>>Type:
print("Hello from Python")Then press Enter. You should see:
Hello from PythonExit Python with:
exit()
or press Ctrl+Z then Enter.
If python does not work but py does, you can use:
py -3to start Python 3, and:
py -3.11to 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
- Go to https://www.python.org/downloads/macos/
- 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
- Double-click the
.pkgfile in your Downloads folder. - Follow the installer steps, accept the license, and keep default installation options.
- At the end it should say installation was successful.
Step 3: Verify
Open Terminal and run:
python3 --versionExpect:
Python 3.11.5Run Python:
python3You should see the Python prompt:
>>>Test it:
print("Hello from Python on macOS")Exit with:
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:
/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
brew install python
This will install the latest Python 3 (usually aliased as python3 and pip3).
Verify:
python3 --versionInstalling Python on Linux
On Linux, there are two main approaches:
- Use your distribution's package manager.
- Use a version manager like
pyenvfor more control.
For beginners, the package manager is usually enough.
Using the Distribution Package Manager
Ubuntu / Debian
Update package lists:
sudo apt updateInstall Python and pip:
sudo apt install -y python3 python3-pipCheck:
python3 --version
pip3 --versionFedora
sudo dnf install -y python3 python3-pipCheck:
python3 --versionCentOS / RHEL (modern versions)
For newer releases:
sudo dnf install -y python3 python3-pipFor older releases, you may need EPEL or a Software Collection, but for backend beginners, start with a modern distro when possible.
Arch Linux
sudo pacman -S python python-pipCheck:
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:
- You work on projects that require different Python versions.
- Your system Python must stay untouched.
Basic steps (example for Ubuntu):
# 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:
pyenv install 3.11.5
pyenv global 3.11.5Check:
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:
- If Python is on your PATH, you can type
pythonorpython3from any folder. - If not, you must type the full path, like
C:\Python311\python.exe, which is annoying.
Verifying PATH on Each OS
Windows
In Command Prompt:
where pythonIf Python is correctly on PATH, you will see something like:
C:\Users\YourName\AppData\Local\Programs\Python\Python311\python.exeIf you see "INFO: Could not find files" then it is not on PATH.
macOS / Linux
In Terminal:
which python3or
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:
- Re-run the Python installer and make sure to select "Modify" or "Change", then enable "Add python.exe to PATH".
- 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
Pathand click "Edit". - Click "New" and add the folder where
python.exeis 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:
python --versionFixing 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:
export PATH="/usr/local/bin:$PATH"Then reload:
source ~/.bashrcNow:
python3 --versionshould work.
Using Python Interactively and With Files
Once installed, you will use Python in two main ways:
- Interactive mode, where you type commands at the
>>>prompt. - Script mode, where you run a
.pyfile.
Interactive Mode
Useful for quick tests and learning.
Example on any OS:
python
# or on macOS/Linux
python3At the prompt:
2 + 3You should see:
5You can also import modules:
import math
math.sqrt(16)Result:
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:
print("Hello from a Python script!")
Navigate to the folder containing hello.py in your terminal, then run:
- Windows:
python hello.py- macOS / Linux:
python3 hello.pyYou should see:
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:
python --version
shows Python 2.7.x, but you installed Python 3.
Solutions:
- Use
python3instead:
python3 --version- On Windows, use
py:
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:
'python' is not recognized as an internal or external commandor:
command not found: python3Causes:
- Python is not installed.
- Python is installed but not on PATH.
Fix:
- Reinstall Python and ensure the installer option to “Add to PATH” is checked (Windows).
- Manually add the Python install directory to PATH (described above).
- On Linux, install using your package manager.
Problem 3: Multiple Python Installations
You might have:
- A system Python.
- Another Python from an official installer.
- A Python from a version manager like
pyenvor Anaconda.
Symptoms:
- Different terminals give different Python versions.
pythonruns one version,python3another.
Tips:
- Stick to one method for installing Python on each machine while you are learning.
- Use
python3orpy -3explicitly. - On Linux/macOS, check which executable is being used:
which python
which python3On Windows:
where python
where pyProblem 4: Permission Denied on Linux/Mac When Installing Packages
You might see something like:
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:
- Prefer virtual environments for your projects.
- Avoid installing many global packages into the system Python.
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:
- Check whether Python is installed and which version you have.
- Install the latest stable Python 3 on Windows, macOS, or Linux.
- Ensure Python is available from your terminal through the PATH.
- Start Python in interactive mode and run basic code.
- Run a simple
hello.pyscript from the command line. - Recognize and fix common installation and PATH issues.
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
KAHIBARO