Skip to main content

Python Packaging

Python Packaging is a core Python concept covering master Python packaging. Learn pip, virtual environments, requirements.txt, project structure, and how to publish your own packages to PyPI. This topic is essential for academic learning, board exam preparation, and developing optimized real-world code.

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?

ProblemSolution
Project A needs Django 3.2, Project B needs Django 5.0Separate venvs — no conflict
You install something experimental and break system PythonThe venv is just a folder — delete it and start fresh
Your teammate gets different versions than youShare 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

PackagePurposeInstall
requestsHTTP requestspip install requests
pandasData analysis, CSV/Excelpip install pandas
numpyNumerical computing, arrayspip install numpy
flaskLightweight web frameworkpip install flask
djangoFull-featured web frameworkpip install django
pytestTesting frameworkpip install pytest
beautifulsoup4HTML/XML parsingpip install beautifulsoup4
matplotlibCharts and plotspip install matplotlib
pillowImage processingpip 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:

  1. python -m build creates .tar.gz (source) and .whl (wheel) files in dist/
  2. twine uploads these files to PyPI
  3. Anyone can now run pip install my-package

Visual Logic: Packaging Workflow


Sample Dry Run

Scenario: Setting up a new project

StepCommandResult
1mkdir my_project && cd my_projectProject directory created
2python -m venv venvvenv/ folder appears
3source venv/bin/activatePrompt shows (venv)
4pip install requests pandasPackages installed in venv
5pip freeze > requirements.txtrequirements.txt created
6pip install -r requirements.txtReproduces exact environment
7deactivateBack 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.txt for 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."


← Back: HTTP Requests | Next: Testing →

📍 Visit Us

🏫 VD Computer Tuition Surat

VD Computer Tuition
📍 Address
2/66 Faram Street, Rustompura
Surat395002, Gujarat, India
📞 Phone / WhatsApp
+91 84604 41384
🌐 Website

Computer Classes & Tuition — Areas We Serve in Surat

AdajanAlthanAmroliAthwaAthwalinesBhagalBhatarBhestanCanal RoadChowkCitylightDumasGaurav PathGhod Dod RoadHaziraJahangirpuraKamrejKapodraKatargamLimbayatMagdallaMajura GateMota VarachhaNanpuraNew CitylightOlpadPalPandesaraParle PointPiplodPunaRanderRing RoadRustampuraSachinSalabatpuraSarthanaSosyo CircleUdhnaVarachhaVed RoadVesuVIP Road
📞 Call Sir💬 WhatsApp Sir