pip downloads packages from PyPI, or another configured index, and installs them into your Python environment's site-packages directory, making them importable. It's not part of the Python language itself — it's a separate command-line tool, though it ships bundled with modern Python installations. Installing from a requirements file installs every package and version listed there, which is the standard way to reproduce a consistent set of dependencies across machines, and freezing the current environment generates that file from whatever's currently installed.
1Understanding pip
pip downloads packages from PyPI, or another configured index, and installs them into your Python environment's site-packages directory, making them importable. It's not part of the Python language itself — it's a separate command-line tool, though it ships bundled with modern Python installations. Installing from a requirements file installs every package and version listed there, which is the standard way to reproduce a consistent set of dependencies across machines, and freezing the current environment generates that file from whatever's currently installed.
Install packages inside a virtual environment, created with venv or a similar tool, not into your system-wide Python installation — this keeps each project's dependencies isolated and avoids version conflicts between unrelated projects.
# Run from a terminal, not inside a Python script:
# pip install requests
import requests
print(requests.__name__)2Practical Example
Here is a real-world application of pip showing how it is used in production Python code.
# requirements.txt
# requests==2.31.0
# numpy==1.26.0
# Install everything listed, from a terminal:
# pip install -r requirements.txt3Best Practices
Follow these guidelines when working with pip:
1. Use a virtual environment per project instead of installing packages globally, to keep dependencies isolated
2. Pin exact versions in a requirements file, or use a lockfile-based tool, for reproducible installs across machines and CI
3. Upgrade pip itself occasionally, since it's versioned and updated separately from Python
Tip: Install packages inside a virtual environment, created with venv or a similar tool, not into your system-wide Python installation — this keeps each project's dependencies isolated and avoids version conflicts between unrelated projects.
# Run from a terminal, not inside a Python script:
# pip install requests
import requests
print(requests.__name__)