Installation & Environment Setup

0/4 in this phase0/54 across the roadmap

📖 Concept

Setting up Python correctly is crucial for avoiding dependency conflicts and ensuring reproducible environments. The most common pain point for beginners is dependency management — different projects need different package versions.

Installing Python:

Method Platform Notes
python.org installer All Official, simple
pyenv macOS/Linux Manage multiple Python versions
Homebrew macOS brew install python
Microsoft Store Windows Convenient but limited
deadsnakes PPA Ubuntu Latest Python versions

Virtual Environments are isolated Python installations that prevent package conflicts between projects. Think of them as separate "sandboxes" for each project.

Tools for virtual environments:

Tool Command Best For
venv python -m venv .venv Built-in, simple projects
virtualenv virtualenv .venv More features than venv
conda conda create -n myenv Data science, non-Python deps
Poetry poetry init Modern dependency management
uv uv venv Fast Rust-based tool (2024+)

Package management:

  • pip — default package installer (pip install package)
  • pip-tools — pin exact versions (pip-compile requirements.in)
  • Poetry — dependency resolution + packaging (poetry add package)
  • uv — drop-in pip replacement, 10-100x faster

IDE recommendations:

  • VS Code with Python extension — most popular, free, great debugging
  • PyCharm — full-featured Python IDE, excellent refactoring
  • Jupyter Notebook/Lab — interactive computing, data science

💻 Code Example

codeTap to expand ⛶
1# ============================================================
2# Setting up Python with pyenv (macOS/Linux)
3# ============================================================
4
5# Install pyenv
6# macOS: brew install pyenv
7# Linux: curl https://pyenv.run | bash
8
9# List available Python versions
10# pyenv install --list | grep "3.12"
11
12# Install a specific version
13# pyenv install 3.12.1
14
15# Set global default
16# pyenv global 3.12.1
17
18# Set local version for a project
19# pyenv local 3.11.7
20
21# ============================================================
22# Virtual Environments with venv (built-in)
23# ============================================================
24
25# Create a virtual environment
26# python -m venv .venv
27
28# Activate it
29# macOS/Linux: source .venv/bin/activate
30# Windows: .venv\Scripts\activate
31
32# Your prompt changes to show the active env:
33# (.venv) $ python --version
34
35# Install packages in the virtual environment
36# pip install requests flask
37
38# Save dependencies
39# pip freeze > requirements.txt
40
41# Install from requirements file
42# pip install -r requirements.txt
43
44# Deactivate when done
45# deactivate
46
47# ============================================================
48# Modern setup with uv (recommended for 2024+)
49# ============================================================
50
51# Install uv
52# curl -LsSf https://astral.sh/uv/install.sh | sh
53
54# Create project with uv
55# uv init my-project
56# cd my-project
57
58# Add dependencies
59# uv add requests flask sqlalchemy
60
61# Run scripts
62# uv run python main.py
63
64# ============================================================
65# Project structure best practice
66# ============================================================
67
68# my-project/
69# ├── .venv/ # Virtual environment (git-ignored)
70# ├── src/
71# │ └── my_project/
72# │ ├── __init__.py
73# │ ├── main.py
74# │ └── utils.py
75# ├── tests/
76# │ ├── __init__.py
77# │ └── test_main.py
78# ├── pyproject.toml # Project metadata & dependencies
79# ├── requirements.txt # Pinned dependencies (if using pip)
80# ├── .gitignore
81# └── README.md
82
83# ============================================================
84# pyproject.toml example (modern Python packaging)
85# ============================================================
86
87# [project]
88# name = "my-project"
89# version = "0.1.0"
90# description = "My awesome Python project"
91# requires-python = ">=3.10"
92# dependencies = [
93# "requests>=2.31.0",
94# "flask>=3.0.0",
95# ]
96#
97# [project.optional-dependencies]
98# dev = [
99# "pytest>=7.0",
100# "ruff>=0.1.0",
101# "mypy>=1.0",
102# ]
103
104# ============================================================
105# .gitignore essentials for Python
106# ============================================================
107# .venv/
108# __pycache__/
109# *.pyc
110# .env
111# *.egg-info/
112# dist/
113# build/
114# .mypy_cache/
115# .pytest_cache/

🏋️ Practice Exercise

Exercises:

  1. Install Python 3.12+ using pyenv (or your platform's preferred method). Verify with python --version.

  2. Create a new project directory with a virtual environment using python -m venv .venv. Activate it, install requests and rich, then freeze dependencies to requirements.txt.

  3. Write a pyproject.toml for a sample project with at least 3 dependencies and separate dev dependencies.

  4. Try uv: install it, create a new project with uv init, add a dependency, and run a simple script.

  5. Set up VS Code for Python: install the Python extension, configure the interpreter to your venv, and verify that linting and IntelliSense work.

  6. Create a proper .gitignore for a Python project. Explain why each entry is important.

⚠️ Common Mistakes

  • Installing packages globally with pip install instead of using a virtual environment. This leads to version conflicts between projects.

  • Forgetting to activate the virtual environment before installing packages — packages end up in the global Python instead of the project's .venv.

  • Committing .venv/ or __pycache__/ to git. These should always be in .gitignore.

  • Using pip freeze > requirements.txt without a virtual environment — it dumps ALL globally installed packages, not just project dependencies.

  • Not pinning dependency versions. requests today might be 2.31, but requests tomorrow might break your code. Use == or >=,< version specifiers.

💼 Interview Questions

🎤 Mock Interview

Practice a live interview for Installation & Environment Setup