Installation & Environment Setup

Get Python installed and set up your development environment

Introduction

Before you can start writing Python code, you need to have Python installed on your system and understand how to manage your development environment. This lesson will walk you through installing Python on Linux, and then show you how to create and manage virtual environments.

Check if Python is Already Installed

Most Linux distributions come with Python pre-installed. Let's first check if Python is already available on your system:

python3 --version

If Python is installed, you'll see output like: Python 3.10.12 or similar. Python 3.9 or higher is recommended for this course because previous versions are reaching end of life.

python --version

Note: On Linux, use python3 explicitly, as python may refer to Python 2 (which is deprecated). But if you are using an up-to-date distribution, python will often point to Python 3 as well.

Installing Python on Linux

Ubuntu/Debian

On Ubuntu or Debian-based systems, use apt to install Python:

# Update package list
sudo apt update

# Install Python 3 and pip
sudo apt install python3 python3-pip python3-venv

# Verify installation
python3 --version
pip3 --version

Fedora/CentOS/RHEL

On Fedora, CentOS, or RHEL systems, use dnf or yum:

# Fedora (using dnf)
sudo dnf install python3 python3-pip python3-venv

# CentOS/RHEL (using yum)
sudo yum install python3 python3-pip python3-venv

# Verify installation
python3 --version
pip3 --version

Arch Linux

On Arch Linux, use pacman:

sudo pacman -S python python-pip

# Verify installation
python --version
pip --version
Installing a Specific Python Version

If you need a specific Python version, you can use deadsnakes PPA on Ubuntu/Debian, or compile from source. For most beginners, the default version from your package manager is perfectly fine.

Installing Python on macOS

Modern macOS versions do not come with Python pre-installed. Here are two recommended approaches:

Option 1: Using Homebrew (Recommended)

Homebrew is the most popular package manager for macOS:

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

# Install Python
brew install python3

# Verify installation
python3 --version

Option 2: Official Python Installer

Download the official installer from python.org:

  • Visit python.org/downloads
  • Download the latest Python 3.x installer
  • Run the installer and follow the instructions
  • The installer will automatically configure your PATH

Installing Python on Windows

Windows does not come with Python pre-installed. Here’s how to set it up safely and correctly:

  1. Visit python.org/downloads/windows
  2. Download the latest Python 3.x installer (choose "Windows Installer (64-bit)" for most modern computers).
  3. Run the downloaded installer.
    • Important: On the first screen, check the box that says "Add Python to PATH".
    • This lets you run python from the Command Prompt anywhere.
  4. Click "Install Now" and follow the prompts to complete installation.
  5. After installation, open Command Prompt (cmd) and type:
    python --version
    You should see something like Python 3.11.0 or similar.
When double-clicking .py files, they may open and close quickly. It's better to run them using the Command Prompt with python filename.py.

What is a Virtual Environment?

A virtual environment is an isolated Python environment that allows you to install packages separately for different projects, think of it as a separate "workspace" for each project that prevents package conflicts between different Python applications.

Why Use Virtual Environments?

  • Isolation: Each project has its own dependencies
  • Version Control: Different projects can use different package versions
  • Clean System: Keeps your system Python installation clean
  • Reproducibility: Makes it easier to share projects with exact dependencies

Without Virtual Environments

  • All packages installed globally
  • Package conflicts between projects
  • Difficult to track project dependencies
  • Can break system tools that depend on specific versions

Using Python's Built-in venv Module

Python 3.3+ includes the venv module, which is the recommended way to create virtual environments. No additional installation is needed!

Step 1: Create a Virtual Environment

Navigate to your project directory and create a virtual environment:

# Navigate to your project directory
cd ~/my-python-project

# Create a virtual environment (replace '.venv' with your desired name)
python3 -m venv .venv

This creates a new directory called .venv containing the virtual environment. Common names are .venv, venv, or env.

Step 2: Activate the Virtual Environment

To use the virtual environment, you need to activate it. On Linux and macOS:

source .venv/bin/activate

After activation, you'll see (.venv) at the beginning of your command prompt, indicating that the virtual environment is active.

Note for macOS users: The command is the same as Linux: source .venv/bin/activate

Step 3: Verify the Virtual Environment

Check that you're using the virtual environment's Python:

# Check Python location (should point to .venv)
which python
# Output: /path/to/my-python-project/.venv/bin/python

# Check Python version
python --version

Step 4: Install Packages

pip (Pip Installs Packages) is Python's built-in package manager. It connects to the Python Package Index (PyPI), a public repository with hundreds of thousands of open-source libraries, downloads, installs, and manages them for you. When your virtual environment is active, pip installs packages only inside that environment, keeping your projects isolated from each other.

With the virtual environment activated, install packages using pip:

# Install a package (example: requests)
pip install requests

# Install multiple packages
pip install requests numpy pandas

# Install from requirements file (we'll cover this later)
pip install -r requirements.txt

Step 5: Deactivate the Virtual Environment

When you're done working, deactivate the virtual environment:

deactivate

This command works the same on Linux, macOS, and Windows. Your prompt will return to normal.

Installing and Using virtualenv (Alternative)

virtualenv is an older tool that predates the built-in venv module. While venv is recommended for Python 3.3+, virtualenv offers some additional features and works with older Python versions.

Install virtualenv

Install virtualenv using pip:

# Install virtualenv globally (or in user space)
pip3 install --user virtualenv

# Or install globally (requires sudo)
sudo pip3 install virtualenv

# Verify installation
virtualenv --version

Create and Use a Virtual Environment

The usage is very similar to venv:

# Create a virtual environment
virtualenv .venv

# Create with specific Python version (if multiple versions installed)
virtualenv --python=python3.12 .venv

# Activate (Linux/macOS)
source .venv/bin/activate

# Deactivate
deactivate
Recommendation

For most users, Python's built-in venv module is sufficient and recommended. Use virtualenv only if you need its additional features or are working with Python 2 (which is deprecated).

Quick Reference: Virtual Environment Workflow

# 1. Create project directory
mkdir my-python-project
cd my-python-project

# 2. Create virtual environment
python3 -m venv .venv

# 3. Activate virtual environment
source .venv/bin/activate  # Linux/macOS

# 4. Install packages
pip install package-name

# 5. Work on your project
python script.py

# 6. Deactivate when done
deactivate

Best Practices

✓ Do
  • Create a virtual environment for each project
  • Name your venv directory clearly (venv or .venv)
  • Add venv/ to your .gitignore
  • Activate the venv before installing packages
  • Keep your requirements.txt updated
✗ Don't
  • Install packages globally unless necessary
  • Commit the venv directory to version control
  • Share venv directories between projects
  • Forget to activate the venv before working
  • Mix system and venv package installations

Key Takeaways

  • Python 3.9+ is recommended for this course; most Linux/macOS systems have Python pre-installed
  • Virtual environments isolate project dependencies, preventing conflicts between different projects
  • Use venv (built-in with Python 3.3+) to create virtual environments - no additional installation needed
  • Always activate your virtual environment before installing packages or running code
  • Common workflow: create .venv → activate → install packages → work → deactivate
  • Best practice: add .venv/ directory to .gitignore and never commit it to version control
What's Next?

Great! You now have Python installed and understand how to work with virtual environments. You're ready to start writing Python code! In the next lessons, we'll cover:

  • Python syntax basics and structure
  • Variables and data types
  • Working with strings and numbers
  • Control flow (if/else, loops)
  • Functions and modules