🌱 Junior Track 15 of 15

15. Virtual Environments & Dependency Management

Essential for professional development: `venv`, `source .venv/bin/activate`, `pip install`, `pip freeze > requirements.txt`, `pip install -r`, and modern `pyproject.toml` setup.

👁️ Views: 0
# ==============================================================================
# JUNIOR PYTHON TRACK 15 — VIRTUAL ENVIRONMENTS & PIP COMMANDS
# ==============================================================================

# Step 1: Create Virtual Environment
$ python3 -m venv .venv

# Step 2: Activate Environment
# Linux / macOS (bash & zsh):
$ source .venv/bin/activate

# Windows (Command Prompt):
C:\> .venv\Scripts\activate.bat

# Windows (PowerShell):
PS C:\> .venv\Scripts\Activate.ps1

# Step 3: Install Third-Party Packages
(.venv) $ pip install flask pytest requests

# Step 4: Lock Dependencies into requirements.txt
(.venv) $ pip freeze > requirements.txt

# Step 5: Install Dependencies in a New System / Environment
(.venv) $ pip install -r requirements.txt

# Step 6: Deactivate Virtual Environment
(.venv) $ deactivate
🌱 Junior Track 15 Master Reference

15. Virtual Environments & Dependency Management Reference

1️⃣ Virtual Environment OS Activation Matrix

Operating System Shell Activation Command
Linux / macOS bash / zsh source .venv/bin/activate
Windows Command Prompt (cmd) .venv\Scripts\activate.bat
Windows PowerShell .venv\Scripts\Activate.ps1
Any OS Active shell session deactivate

2️⃣ Essential `pip` Package Commands

  • pip install <pkg>: Downloads and installs a package from PyPI.
  • pip install <pkg>==1.2.3: Installs an exact version.
  • pip uninstall <pkg>: Removes an installed package.
  • pip list: Displays all installed packages in active environment.
  • pip freeze > requirements.txt: Saves exact package versions to a text file.
  • pip install -r requirements.txt: Installs all listed requirements.

3️⃣ requirements.txt vs Modern `pyproject.toml`

While requirements.txt is standard for locking application deployment dependencies, modern Python projects use pyproject.toml (PEP 621) to specify project metadata, build systems (hatchling, setuptools, poetry), and dependencies in a unified file.

# pyproject.toml
[project]
name = "my_teach_cloud_app"
version = "1.0.0"
dependencies = [
    "flask>=3.0.0",
    "pytest>=8.0.0"
]

⭐ Rate Track 15: Virtual Environments (1 to 5 Stars)

Your rating is stored directly in the database. Average: 5.0 / 5 (1 rating)

★ ★ ★ ★ ★
← 14. Modules & Packages 🌱 Junior Hub 16. Standard Library →