DocsHub
Modules

Packages & pip

Learn how to install third-party packages, manage dependencies, and use virtual environments in Python.

Packages & pip

Python's standard library is powerful — but the real strength of Python is its ecosystem. Hundreds of thousands of third-party packages are available for free, built by the community, covering everything from web development to data science to machine learning.

pip is Python's package manager. It lets you install, update, and remove these packages with a single command.


What is a package?

A package is a collection of modules bundled together and published to the Python Package Index — pypi.org. When someone builds something useful, they publish it there so anyone can install it.

Some of the most popular packages:

PackageUse for
requestsMaking HTTP requests
pandasData analysis
numpyNumerical computing
fastapiBuilding APIs
djangoFull web framework
sqlalchemyDatabase toolkit
pillowImage processing
pytestTesting
pydanticData validation
richBeautiful terminal output

pip — installing packages

Open your terminal and run:

pip install requests

That's it. pip downloads requests from PyPI and installs it. Now you can import it:

import requests

response = requests.get("https://api.github.com")
print(response.status_code)   # 200

Install a specific version:

pip install requests==2.31.0

Install the latest version of an already installed package:

pip install --upgrade requests

Uninstall a package:

pip uninstall requests

See all installed packages:

pip list

See details about one package:

pip show requests

Output:

Name: requests
Version: 2.31.0
Summary: Python HTTP for Humans.
Author: Kenneth Reitz
Location: /usr/local/lib/python3.12/site-packages
Requires: certifi, charset-normalizer, idna, urllib3

Virtual environments

Here is a problem. You have two projects:

  • Project A needs requests==2.28.0
  • Project B needs requests==2.31.0

If you install globally, one of them will break. The solution is a virtual environment — an isolated Python environment for each project. Each project gets its own packages, its own versions, completely separate from everything else.


Creating a virtual environment

# create it
python3 -m venv venv

This creates a folder called venv in your current directory containing a fresh isolated Python installation.

The folder is usually named venv or .venv by convention. Call it whatever you want — the name does not matter technically.


Activating the virtual environment

macOS / Linux:

source venv/bin/activate

Windows:

venv\Scripts\activate

Once activated, your terminal prompt changes to show the environment name:

(venv) ali@macbook project %

Now any pip install goes into this environment only — not the global Python.


Deactivating

deactivate

Your terminal goes back to normal.


The full workflow

# 1. create your project folder
mkdir my_project
cd my_project

# 2. create a virtual environment
python3 -m venv venv

# 3. activate it
source venv/bin/activate   # macOS/Linux
# venv\Scripts\activate    # Windows

# 4. install your packages
pip install requests fastapi pandas

# 5. work on your project...

# 6. deactivate when done
deactivate

requirements.txt — tracking dependencies

When you share your project with someone else — or deploy it to a server — they need to install the same packages you used. requirements.txt is the standard way to track this.

Generate it from your current environment:

pip freeze > requirements.txt

This creates a file listing every installed package and its exact version:

certifi==2024.2.2
charset-normalizer==3.3.2
fastapi==0.111.0
idna==3.7
pandas==2.2.2
requests==2.31.0
starlette==0.37.2
urllib3==2.2.1

Install from requirements.txt:

pip install -r requirements.txt

This is how someone else sets up your project:

git clone https://github.com/you/your-project
cd your-project
python3 -m venv venv
source venv/bin/activate
pip install -r requirements.txt

Always add venv/ to your .gitignore file. Never commit the virtual environment folder to git — it is large, machine-specific, and unnecessary since requirements.txt captures everything needed to recreate it.

# .gitignore
venv/
.venv/
__pycache__/
*.pyc

pyproject.toml — the modern standard

Newer Python projects use pyproject.toml instead of requirements.txt. It is more powerful — it describes the whole project, not just dependencies.

[project]
name = "my-project"
version = "1.0.0"
description = "A sample Python project"
requires-python = ">=3.10"

dependencies = [
    "requests>=2.31.0",
    "fastapi>=0.111.0",
    "pandas>=2.2.0",
]

[project.optional-dependencies]
dev = [
    "pytest>=8.0.0",
    "black>=24.0.0",
]

Tools like uv, poetry, and hatch use this format. For simple projects, requirements.txt is still perfectly fine.


uv — the modern fast package manager

uv is a newer, extremely fast Python package manager written in Rust. It is becoming the standard in 2025-2026 for many projects because it is 10-100x faster than pip.

# install uv
curl -LsSf https://astral.sh/uv/install.sh | sh

# create a project with uv
uv init my_project
cd my_project

# add a package
uv add requests

# run your script
uv run main.py

uv handles virtual environments automatically — you do not need to create and activate one manually.

For learning and small projects, plain pip with a venv is completely fine. uv becomes more valuable in larger projects where install speed and dependency management matter more.


Useful development packages

These are packages almost every Python developer installs regardless of the project type:

black — auto-formats your code to consistent style:

pip install black
black my_file.py    # formats the file in place

ruff — extremely fast linter and formatter, replacing flake8 and isort:

pip install ruff
ruff check .        # check for issues
ruff format .       # format code

pytest — the standard testing framework:

pip install pytest
pytest              # runs all tests

rich — beautiful terminal output:

from rich import print
from rich.table import Table

print("[bold green]Hello[/bold green], [blue]world![/blue]")

python-dotenv — load environment variables from a .env file:

from dotenv import load_dotenv
import os

load_dotenv()
api_key = os.environ.get("API_KEY")

A real example

A complete project setup from scratch:

# create project
mkdir weather_app
cd weather_app

# set up environment
python3 -m venv venv
source venv/bin/activate

# install packages
pip install requests python-dotenv rich

# save dependencies
pip freeze > requirements.txt

main.py:

import os
import requests
from dotenv import load_dotenv
from rich import print
from rich.table import Table

load_dotenv()

API_KEY = os.environ.get("OPENWEATHER_API_KEY")

def get_weather(city):
    url = f"https://api.openweathermap.org/data/2.5/weather"
    params = {
        "q": city,
        "appid": API_KEY,
        "units": "metric"
    }
    response = requests.get(url, params=params)
    return response.json()

def show_weather(city):
    data = get_weather(city)

    table = Table(title=f"Weather in {city}")
    table.add_column("Property", style="cyan")
    table.add_column("Value", style="green")

    table.add_row("Temperature", f"{data['main']['temp']}°C")
    table.add_row("Feels Like", f"{data['main']['feels_like']}°C")
    table.add_row("Humidity", f"{data['main']['humidity']}%")
    table.add_row("Condition", data['weather'][0]['description'].title())

    print(table)

if __name__ == "__main__":
    show_weather("Lahore")

.env:

OPENWEATHER_API_KEY=your_api_key_here

.gitignore:

venv/
.env
__pycache__/
*.pyc

Summary

CommandWhat it does
pip install packageInstall a package
pip install package==1.0.0Install specific version
pip install --upgrade packageUpgrade a package
pip uninstall packageRemove a package
pip listList installed packages
pip freeze > requirements.txtSave dependencies
pip install -r requirements.txtInstall from file
python3 -m venv venvCreate virtual environment
source venv/bin/activateActivate (macOS/Linux)
venv\Scripts\activateActivate (Windows)
deactivateDeactivate environment

On this page