Managing Packages with pip and requirements.txt
15 min · Beginner
Need help? Ask a tutor on WhatsApp (opens in a new tab)Showing steps for Windows. They are marked “On Windows” below.
pip is Python’s official package installer, connected to the Python Package Index (PyPI). Combining pip with a requirements.txt file lets you freeze and share exact project dependencies so any developer or CI/CD pipeline can replicate your setup with one command.
Installing and Upgrading Packages
pip is Python's package installer. It downloads a library from PyPI, the public package index, and places it in the site-packages folder of the active environment.
With your environment active, install a library by name. Add == and a version to pin an exact release. Using python -m pip guarantees you use the pip of the active Python.
On Windows
In PowerShell, the packages land in .venv\Lib\site-packages.
(.venv) PS C:\Users\Priya\shop-app> python -m pip install httpx==0.27.0Collecting httpx==0.27.0 Downloading httpx-0.27.0-py3-none-any.whl (75 kB) Installing collected packages: sniffio, idna, h11, certifi, httpcore, anyio, httpx Successfully installed anyio-4.4.0 certifi-2024.7.4 h11-0.14.0 httpcore-1.0.5 httpx-0.27.0 idna-3.7 sniffio-1.3.1
Inspecting Installed Packages
Check what the environment contains. pip list shows every package and version; pip show gives details for one.
On Windows
Location shows the .venv\Lib\site-packages folder.
(.venv) PS C:\Users\Priya\shop-app> python -m pip show httpxName: httpx Version: 0.27.0 Summary: The next generation HTTP client. Location: C:\Users\Priya\shop-app\.venv\Lib\site-packages Requires: anyio, certifi, httpcore, idna, sniffio
Freezing Dependencies with requirements.txt
requirements.txt is a plain text file that lists a project's packages, one per line, with pinned versions. pip install -r reads it to rebuild the same environment.
To share your project, save the installed packages into a file. Teammates then install exactly the same versions.
On Windows
Windows PowerShell 5.1 writes > output in UTF-16, which pip cannot read. Save with Out-File -Encoding utf8 instead.
(.venv) PS C:\Users\Priya\shop-app> python -m pip freeze | Out-File -Encoding utf8 requirements.txt(.venv) PS C:\Users\Priya\shop-app> Get-Content requirements.txtOutputanyio==4.4.0 certifi==2024.7.4 h11==0.14.0 httpcore==1.0.5 httpx==0.27.0 idna==3.7 sniffio==1.3.1
Installing from requirements.txt
After cloning a project, create a fresh environment, activate it, and install from the file. One command rebuilds everything.
On Windows
In PowerShell, activate with Activate.ps1, then install.
PS C:\Users\Priya\shop-app> py -m venv .venvPS C:\Users\Priya\shop-app> .venv\Scripts\Activate.ps1PS C:\Users\Priya\shop-app> python -m pip install -r requirements.txtOutputCollecting anyio==4.4.0 (from -r requirements.txt (line 1)) Downloading anyio-4.4.0-py3-none-any.whl (86 kB) Successfully installed anyio-4.4.0 certifi-2024.7.4 h11-0.14.0 httpcore-1.0.5 httpx-0.27.0 idna-3.7 sniffio-1.3.1
Example
requirements = """fastapi==0.110.0pydantic==2.6.4uvicorn==0.28.0"""
packages = {}for line in requirements.strip().split("\n"): name, version = line.split("==") packages[name] = version
for pkg, ver in packages.items(): print(f"{pkg}: {ver}")fastapi: 0.110.0 pydantic: 2.6.4 uvicorn: 0.28.0
What this code does
- The
requirements.txtformat usespackage_name==version. - Parsing lines allows tools to verify matching dependencies programmatically.
What's next
You finished the preview of Terminal & Virtual Environments
Python Programming has 21 sections, 110 modules and 609 lessons. An account opens every lesson and saves your progress.
Next step
Check your understanding
7 quick questions