Ojasa Mirai

Ojasa Mirai

Python

Loading...

Learning Level

🟢 BeginneršŸ”µ Advanced
Modules Import BasicsCreating ModulesImport StatementsRelative ImportsImport PathsPackages StructureNamespace PackagesPip & DependenciesModule Performance
Python/Modules Packages/Pip And Dependencies

šŸ“¦ Pip & Dependencies — Managing Python Packages

Pip is Python's package manager. It lets you download and install packages from PyPI (Python Package Index) and manage your project dependencies.


šŸŽÆ Installing Packages with Pip

Pip is the standard tool for installing Python packages.

# Basic install
pip install requests

# Install specific version
pip install numpy==1.21.0

# Install multiple packages
pip install flask django pytest

# Install with version constraints
pip install "pandas>=1.0,<2.0"

# Upgrade installed package
pip install --upgrade requests
pip install -U requests  # Shorthand

šŸ’” Common Pip Commands

Finding packages:

# Search for a package
pip search flask  # (deprecated, use PyPI website instead)

# Show package information
pip show numpy
# Output:
# Name: numpy
# Version: 1.21.0
# Location: /usr/local/lib/python3.9/site-packages

Viewing installed packages:

# List all installed packages
pip list

# List with specific format
pip list --format=json

# Check if a package is installed
pip show pandas

Uninstalling packages:

# Remove a package
pip uninstall requests

# Remove multiple packages
pip uninstall flask django pytest

# Uninstall without confirmation
pip uninstall -y requests

šŸ“‹ Requirements Files

Manage dependencies with `requirements.txt`.

Creating requirements.txt:

# Generate from current environment
pip freeze > requirements.txt

# Manual format
flask==2.0.1
requests>=2.26.0
pytest>=6.2.0
numpy>=1.20.0
pandas>=1.2.0

Using requirements.txt:

# Install all dependencies
pip install -r requirements.txt

# Install dev dependencies
pip install -r requirements-dev.txt

# Install with constraints
pip install -r requirements.txt --upgrade

Different requirements files:

project/
ā”œā”€ā”€ requirements.txt           # Core dependencies
ā”œā”€ā”€ requirements-dev.txt       # Development tools
ā”œā”€ā”€ requirements-test.txt      # Testing tools
└── requirements-prod.txt      # Production settings

requirements.txt:

flask==2.0.1
sqlalchemy==1.4.0
requests>=2.26.0

requirements-dev.txt:

-r requirements.txt    # Include core requirements
pytest==6.2.0
black==21.7b0
flake8==3.9.0
pytest-cov>=2.12.0
# Install production dependencies
pip install -r requirements.txt

# Install development dependencies
pip install -r requirements-dev.txt

šŸ—ļø Virtual Environments

Use virtual environments to isolate project dependencies.

# Create virtual environment
python -m venv venv

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

# Activate (Windows)
venv\Scripts\activate

# Install packages in virtual environment
pip install flask requests

# Deactivate virtual environment
deactivate

Why virtual environments?

# Without venv: packages conflict
# Project A needs: flask==1.0
# Project B needs: flask==2.0
# Can't have both installed globally!

# With venv: each project isolated
project_a/venv/  # has flask==1.0
project_b/venv/  # has flask==2.0
# Both work simultaneously!

šŸ“Š Project Setup Example

myproject/
ā”œā”€ā”€ venv/                    # Virtual environment (don't commit!)
ā”œā”€ā”€ src/
│   └── myapp/
│       ā”œā”€ā”€ __init__.py
│       └── main.py
ā”œā”€ā”€ tests/
│   └── test_main.py
ā”œā”€ā”€ requirements.txt
└── README.md

setup.sh:

#!/bin/bash

# Create and activate virtual environment
python -m venv venv
source venv/bin/activate  # On Windows: venv\Scripts\activate

# Install dependencies
pip install -r requirements.txt

# Run application
python src/myapp/main.py

šŸ’» Using Installed Packages

# After pip install requests
import requests

response = requests.get("https://api.example.com")
data = response.json()
# After pip install numpy
import numpy as np

array = np.array([1, 2, 3, 4, 5])
mean = np.mean(array)
# After pip install flask
from flask import Flask, jsonify

app = Flask(__name__)

@app.route("/api/data")
def get_data():
    return jsonify({"message": "Hello, World!"})

if __name__ == "__main__":
    app.run(debug=True)

šŸ”„ Version Specifiers

Control which versions of packages are installed.

# Exact version
pip install numpy==1.21.0

# Greater than or equal
pip install "pandas>=1.2.0"

# Less than
pip install "flask<3.0"

# Range
pip install "requests>=2.26.0,<3.0"

# Compatible release
pip install "django~=3.2"  # >= 3.2, < 4.0

# Any version
pip install numpy

šŸŽØ Practical Dependency Examples

Data Science Project:

# requirements.txt
numpy==1.21.0
pandas==1.3.0
matplotlib==3.4.2
scikit-learn==0.24.2
jupyter==1.0.0

Web Application:

# requirements.txt
flask==2.0.1
sqlalchemy==1.4.0
flask-sqlalchemy==2.5.1
flask-cors==3.0.10
python-dotenv==0.19.0
gunicorn==20.1.0

# requirements-dev.txt
-r requirements.txt
pytest==6.2.0
black==21.7b0
flake8==3.9.0

Testing Setup:

# requirements-test.txt
pytest==6.2.0
pytest-cov==2.12.0
pytest-xdist==2.3.0
hypothesis==6.13.1

āš ļø Common Issues

Issue 1: "No module named 'x'"

# āŒ Package not installed
python my_script.py  # Error: ModuleNotFoundError

# āœ… Install the package
pip install requests

# Verify in virtual environment
which python  # Should show venv/bin/python

Issue 2: Version Conflicts

# āŒ Package requires different version
pip install "numpy>=1.20" "numpy<1.21"

# āœ… Use compatible versions
pip install "numpy>=1.20,<2.0"

# Or check compatibility
pip check

Issue 3: Frozen Dependencies

# āŒ requirements.txt with no versions
flask
requests

# āœ… Pin versions for reproducibility
pip freeze > requirements.txt
# Creates exact versions:
# flask==2.0.1
# requests==2.26.0

šŸ”‘ Key Takeaways

  • āœ… Use `pip install package_name` to install packages
  • āœ… Use `requirements.txt` to manage dependencies
  • āœ… Use virtual environments to isolate projects
  • āœ… Pin versions in requirements.txt for reproducibility
  • āœ… Use `pip freeze` to capture current environment
  • āœ… Check compatibility with `pip check`
  • āœ… Always activate virtual environment before installing

Ready to practice? Challenges | Quiz


Resources

Python Docs

Ojasa Mirai

Master AI-powered development skills through structured learning, real projects, and verified credentials. Whether you're upskilling your team or launching your career, we deliver the skills companies actually need.

Learn Deep • Build Real • Verify Skills • Launch Forward

Courses

PythonFastapiReactJSCloud

Ā© 2026 Ojasa Mirai. All rights reserved.

TwitterGitHubLinkedIn