Using `pip` and `requirements.txt` Like a Pro
Using pip and requirements.txt Like a Pro
If you’re working with Python, you’ve likely used pip to install packages. But did you know that using pip efficiently, especially with a requirements.txt file, can make your Python projects cleaner, easier to share, and more professional?
In this blog, you'll learn how to use pip and requirements.txt like a pro—from the basics to smart practices that help you manage dependencies and collaborate with others effectively.
π§° What Is pip?
pip is Python’s default package installer. You can use it to:
-
Install packages from PyPI
-
Upgrade or uninstall packages
-
List installed packages
-
Freeze project dependencies
π¦ Example:
This installs the requests library into your current environment.
π What Is requirements.txt?
A requirements.txt file is a list of packages your project needs. It allows others to install the same dependencies quickly, ensuring your code runs the same everywhere.
π Example requirements.txt:
This file:
-
Ensures consistent versions across machines
-
Is used in production and deployment pipelines
-
Makes teamwork and code sharing easier
✅ Creating a Virtual Environment (Recommended)
First, create a virtual environment so packages are installed only for your project.
Activate it:
-
Windows:
-
macOS/Linux:
π‘ Installing Packages with pip
Install a single package:
Install with version constraints:
✍️ Creating a requirements.txt File
Once your environment has all the packages, run:
This creates a list like:
This ensures anyone using your project installs exactly the same versions.
⬇️ Installing from requirements.txt
To install all packages from the file:
This saves time and avoids missing packages when moving between systems or working in teams.
π Updating requirements.txt
When you install or remove packages, update the file:
You can also manually edit it to:
-
Remove unused packages
-
Change version constraints
-
Keep only top-level dependencies
π§Ό Cleaning Unused Packages (Pro Tip)
If your environment has many unused packages, try using pipreqs:
This will generate a requirements.txt based on the actual imports in your code.
π Best Practices for Using pip & requirements.txt
| Best Practice | Why It Matters |
|---|---|
| Use virtual environments | Keeps projects isolated |
Pin versions in requirements.txt | Prevents future compatibility issues |
Use pip freeze responsibly | Don’t include unused packages |
Keep requirements.txt in Git repo | So others can reproduce your setup |
Use -r requirements.txt to install | Saves time and avoids errors |
Use pip list --outdated | To check for updates safely |
π Upgrading Packages
To upgrade a package to the latest version:
To upgrade everything in requirements.txt (use with caution):
π¦ Pro Tools for Dependency Management
Consider using:
-
pip-tools for smarter
requirements.txtgeneration -
Poetry or Pipenv for more structured dependency + environment management
-
Docker + requirements.txt for reproducible environments
π§ Summary: Mastering pip and requirements.txt
| Command | Description |
|---|---|
pip install package | Install a package |
pip freeze > requirements.txt | Save current packages |
pip install -r requirements.txt | Install packages from file |
pip list --outdated | Show outdated packages |
python -m venv venv | Create virtual environment |
π Conclusion
Whether you’re just starting with Python or building serious projects, using pip and requirements.txt properly will make your development process smoother, more professional, and easier to share.
Start using these tools wisely, and you’ll save yourself from dependency hell, version issues, and frustrating “it works on my machine” problems.
Learn FullStack Python Course
Comments
Post a Comment