Module 7 ยท Lesson 22

pip, Virtual Environments, and Project Structure

๐Ÿ Pythonโฑ 12 min read๐Ÿ“– Core Concept

pip: Python's Package Manager

pip installs packages from PyPI (Python Package Index) โ€” over 450,000 packages. It's the primary way to add third-party libraries to your projects.

# Install a package pip install requests # Install specific version pip install requests==2.31.0 # Install from requirements file pip install -r requirements.txt # Upgrade a package pip install --upgrade requests # Uninstall pip uninstall requests # List installed packages pip list pip freeze # With exact versions (good for requirements.txt) # Generate requirements file pip freeze > requirements.txt # Show package info pip show requests # Search (limited without PyPI web) pip index versions requests # All available versions

pip vs pip3

# On some systems, pip points to Python 2's pip # Always use pip3 if unsure, or use: python3 -m pip install requests # Guaranteed to use Python 3's pip

Virtual Environments

Virtual environments isolate project dependencies. Each project gets its own Python installation and packages โ€” no version conflicts between projects.

# Create a virtual environment python3 -m venv venv # Creates venv/ directory # Activate source venv/bin/activate # macOS/Linux .\venv\Scripts\activate # Windows PowerShell source venv/Scripts/activate # Windows Git Bash # Your prompt changes to: (venv) $ # Now you're working in the isolated environment # Install packages (isolated to this project) pip install flask requests pandas # Deactivate deactivate # Delete environment (just delete the folder) rm -rf venv/

pyproject.toml: Modern Project Configuration

# pyproject.toml โ€” modern way to configure Python projects [build-system] requires = ["setuptools>=61.0"] build-backend = "setuptools.backends.legacy:build" [project] name = "my-project" version = "1.0.0" description = "A cool Python project" requires-python = ">=3.9" dependencies = [ "requests>=2.28.0", "flask>=2.3.0", ] [project.optional-dependencies] dev = [ "pytest>=7.0", "black>=23.0", "mypy>=1.0", ] [tool.black] line-length = 88 [tool.mypy] python_version = "3.11" strict = true

Project Structure Best Practices

# Professional Python project structure my-project/ โ”œโ”€โ”€ src/ โ”‚ โ””โ”€โ”€ mypackage/ โ”‚ โ”œโ”€โ”€ __init__.py โ”‚ โ”œโ”€โ”€ main.py โ”‚ โ””โ”€โ”€ utils.py โ”œโ”€โ”€ tests/ โ”‚ โ”œโ”€โ”€ __init__.py โ”‚ โ”œโ”€โ”€ test_main.py โ”‚ โ””โ”€โ”€ test_utils.py โ”œโ”€โ”€ docs/ โ”œโ”€โ”€ .gitignore โ”œโ”€โ”€ pyproject.toml โ”œโ”€โ”€ README.md โ””โ”€โ”€ requirements.txt # .gitignore for Python projects venv/ __pycache__/ *.pyc *.pyo .env .pytest_cache/ *.egg-info/ dist/ build/

Key Takeaways

Practice Exercises

  1. Create a new project directory, set up a virtual environment, and install 3 packages. Generate requirements.txt.
  2. Create a minimal pyproject.toml for a hypothetical project with 2 dependencies.
  3. Install a package, use it in a script, then create a fresh virtual environment and verify the script fails, then install from requirements.txt and verify it works.
โ† Modules and Imports