Python Packaging
Mentor's Note: Packaging is how Python projects travel. A well-packaged project can be installed by anyone with a single command —
pip install your_project. 📦
The Scenario: The Shipping Warehouse
Imagine you run a shipping warehouse.
- Without Packaging: You hand-deliver each product individually. Chaos. Nothing is tracked. Dependencies get lost.
- With Packaging: Every product is boxed 📦, labeled 🏷️, and logged 📋. You know exactly what's in every shipment and where it goes.
- The Result: Clean, repeatable, shippable deliveries — every time. ✅
What Is pip?
pip is Python's package installer. It downloads and installs packages from the Python Package Index (PyPI) — the official repository of Python libraries.
# Install a package
pip install requests
# Install a specific version
pip install pandas==2.0.0
# Install with minimum version
pip install "flask>=2.3.0"
# Install multiple packages
pip install requests pandas numpy flask
# List installed packages
pip list
# Show package info
pip show requests
# Uninstall a package
pip uninstall requests
Virtual Environments
A virtual environment is an isolated Python environment for each project. Packages installed in one venv do not affect others.
Why Use Virtual Environments?
| Problem | Solution |
|---|---|
| Project A needs Django 3.2, Project B needs Django 5.0 | Separate venvs — no conflict |
| You install something experimental and break system Python | The venv is just a folder — delete it and start fresh |
| Your teammate gets different versions than you | Share requirements.txt for exact reproducibility |
Creating and Using a Virtual Environment
# Create a virtual environment
python -m venv venv
# Activate it:
# On Windows (Command Prompt)
venv\Scripts\activate
# On Windows (PowerShell)
venv\Scripts\Activate.ps1
# On macOS / Linux
source venv/bin/activate
# Your prompt now shows (venv) — everything is isolated
# Install packages inside the venv
pip install requests pandas
# When done, deactivate
deactivate
requirements.txt
A requirements.txt file lists all the packages your project needs.
Creating requirements.txt
# Freeze current environment into a file
pip freeze > requirements.txt
Example requirements.txt
requests==2.31.0
pandas>=2.0.0,<3.0.0
numpy>=1.24.0
flask>=2.3.0
pytest>=7.4.0
Installing from requirements.txt
# Setup
python -m venv venv
source venv/bin/activate
# Install everything at once
pip install -r requirements.txt
Common Useful Packages
| Package | Purpose | Install |
|---|---|---|
| requests | HTTP requests | pip install requests |
| pandas | Data analysis, CSV/Excel | pip install pandas |
| numpy | Numerical computing, arrays | pip install numpy |
| flask | Lightweight web framework | pip install flask |
| django | Full-featured web framework | pip install django |
| pytest | Testing framework | pip install pytest |
| beautifulsoup4 | HTML/XML parsing | pip install beautifulsoup4 |
| matplotlib | Charts and plots | pip install matplotlib |
| pillow | Image processing | pip install pillow |
# Quick examples of each:
import requests # requests.get("https://api.github.com")
import pandas as pd # df = pd.read_csv("data.csv")
import numpy as np # arr = np.array([1, 2, 3])
from flask import Flask # app = Flask(__name__)
import pytest # pytest.main()
from bs4 import BeautifulSoup # soup = BeautifulSoup(html, "html.parser")
import matplotlib.pyplot as plt # plt.plot([1, 2, 3])
from PIL import Image # img = Image.open("photo.jpg")
Creating Your Own Package
Project Structure
my_package/
├── pyproject.toml # Project metadata and dependencies
├── README.md # Documentation
├── src/
│ └── my_package/ # Your actual code
│ ├── __init__.py
│ ├── module1.py
│ └── module2.py
└── tests/
├── __init__.py
└── test_module1.py
pyproject.toml (Modern Standard)
[build-system]
requires = ["setuptools>=61.0"]
build-backend = "setuptools.backends._legacy:_Backend"
[project]
name = "my-package"
version = "0.1.0"
description = "A useful description of my package"
readme = "README.md"
license = {text = "MIT"}
authors = [
{name = "Your Name", email = "[email protected]"}
]
requires-python = ">=3.8"
dependencies = [
"requests>=2.28.0",
"click>=8.0.0",
]
[project.urls]
Homepage = "https://github.com/yourname/my-package"
setup.py (Legacy — Still Common)
from setuptools import setup, find_packages
setup(
name="my-package",
version="0.1.0",
packages=find_packages(where="src"),
package_dir={"": "src"},
install_requires=[
"requests>=2.28.0",
"click>=8.0.0",
],
entry_points={
"console_scripts": [
"my-cli=my_package.cli:main",
],
},
)
Installing Your Package Locally
# Install in development mode — changes take effect immediately
pip install -e .
# Install normally
pip install .
Publishing to PyPI
A conceptual overview of sharing your package with the world.
# 1. Install build tools
pip install build twine
# 2. Build distribution files
python -m build
# 3. Upload to Test PyPI first
twine upload --repository testpypi dist/*
# 4. Upload to real PyPI
twine upload dist/*
What happens:
python -m buildcreates.tar.gz(source) and.whl(wheel) files indist/twineuploads these files to PyPI- Anyone can now run
pip install my-package
Visual Logic: Packaging Workflow
Sample Dry Run
Scenario: Setting up a new project
| Step | Command | Result |
|---|---|---|
| 1 | mkdir my_project && cd my_project | Project directory created |
| 2 | python -m venv venv | venv/ folder appears |
| 3 | source venv/bin/activate | Prompt shows (venv) |
| 4 | pip install requests pandas | Packages installed in venv |
| 5 | pip freeze > requirements.txt | requirements.txt created |
| 6 | pip install -r requirements.txt | Reproduces exact environment |
| 7 | deactivate | Back to system Python |
Pro Tips
- Always use a virtual environment — never install packages globally.
- Add
venv/and__pycache__/to your.gitignore. - Pin exact versions in
requirements.txtfor deployment; use loose ranges for libraries. - Use
pip install -e .during development so imports always reflect your latest code. - Python 3.12+ has faster venv creation and better dependency resolution.
Interview Tip
"Interviewers ask: 'What's the difference between a virtual environment and a container (Docker)?' A venv isolates Python packages only; Docker isolates the entire OS, including system dependencies, Python version, and network. Use venv for development, Docker for deployment."