Installation & Environment Setup
📖 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
1# ============================================================2# Setting up Python with pyenv (macOS/Linux)3# ============================================================45# Install pyenv6# macOS: brew install pyenv7# Linux: curl https://pyenv.run | bash89# List available Python versions10# pyenv install --list | grep "3.12"1112# Install a specific version13# pyenv install 3.12.11415# Set global default16# pyenv global 3.12.11718# Set local version for a project19# pyenv local 3.11.72021# ============================================================22# Virtual Environments with venv (built-in)23# ============================================================2425# Create a virtual environment26# python -m venv .venv2728# Activate it29# macOS/Linux: source .venv/bin/activate30# Windows: .venv\Scripts\activate3132# Your prompt changes to show the active env:33# (.venv) $ python --version3435# Install packages in the virtual environment36# pip install requests flask3738# Save dependencies39# pip freeze > requirements.txt4041# Install from requirements file42# pip install -r requirements.txt4344# Deactivate when done45# deactivate4647# ============================================================48# Modern setup with uv (recommended for 2024+)49# ============================================================5051# Install uv52# curl -LsSf https://astral.sh/uv/install.sh | sh5354# Create project with uv55# uv init my-project56# cd my-project5758# Add dependencies59# uv add requests flask sqlalchemy6061# Run scripts62# uv run python main.py6364# ============================================================65# Project structure best practice66# ============================================================6768# my-project/69# ├── .venv/ # Virtual environment (git-ignored)70# ├── src/71# │ └── my_project/72# │ ├── __init__.py73# │ ├── main.py74# │ └── utils.py75# ├── tests/76# │ ├── __init__.py77# │ └── test_main.py78# ├── pyproject.toml # Project metadata & dependencies79# ├── requirements.txt # Pinned dependencies (if using pip)80# ├── .gitignore81# └── README.md8283# ============================================================84# pyproject.toml example (modern Python packaging)85# ============================================================8687# [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# ]103104# ============================================================105# .gitignore essentials for Python106# ============================================================107# .venv/108# __pycache__/109# *.pyc110# .env111# *.egg-info/112# dist/113# build/114# .mypy_cache/115# .pytest_cache/
🏋️ Practice Exercise
Exercises:
Install Python 3.12+ using pyenv (or your platform's preferred method). Verify with
python --version.Create a new project directory with a virtual environment using
python -m venv .venv. Activate it, installrequestsandrich, then freeze dependencies torequirements.txt.Write a
pyproject.tomlfor a sample project with at least 3 dependencies and separate dev dependencies.Try
uv: install it, create a new project withuv init, add a dependency, and run a simple script.Set up VS Code for Python: install the Python extension, configure the interpreter to your venv, and verify that linting and IntelliSense work.
Create a proper
.gitignorefor a Python project. Explain why each entry is important.
⚠️ Common Mistakes
Installing packages globally with
pip installinstead 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.txtwithout a virtual environment — it dumps ALL globally installed packages, not just project dependencies.Not pinning dependency versions.
requeststoday might be 2.31, butrequeststomorrow might break your code. Use==or>=,<version specifiers.
💼 Interview Questions
🎤 Mock Interview
Practice a live interview for Installation & Environment Setup