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:

pip install requests

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:

flask==2.2.5 requests>=2.28.0 pandas

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.

python -m venv venv

Activate it:

  • Windows:

    venv\Scripts\activate
  • macOS/Linux:

    source venv/bin/activate

πŸ’‘ Installing Packages with pip

Install a single package:

pip install numpy

Install with version constraints:

pip install flask==2.3.0 pip install requests>=2.28.0,<3.0

✍️ Creating a requirements.txt File

Once your environment has all the packages, run:

pip freeze > requirements.txt

This creates a list like:

flask==2.2.5 requests==2.29.0 pandas==2.1.1

This ensures anyone using your project installs exactly the same versions.


⬇️ Installing from requirements.txt

To install all packages from the file:

pip install -r requirements.txt

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:

pip freeze > requirements.txt

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:

pip install pipreqs pipreqs . --force

This will generate a requirements.txt based on the actual imports in your code.


πŸ” Best Practices for Using pip & requirements.txt

Best PracticeWhy It Matters
Use virtual environmentsKeeps projects isolated
Pin versions in requirements.txtPrevents future compatibility issues
Use pip freeze responsiblyDon’t include unused packages
Keep requirements.txt in Git repoSo others can reproduce your setup
Use -r requirements.txt to installSaves time and avoids errors
Use pip list --outdatedTo check for updates safely

πŸ”„ Upgrading Packages

To upgrade a package to the latest version:

pip install --upgrade flask

To upgrade everything in requirements.txt (use with caution):

pip list --outdated # then update versions manually or use pip-tools (advanced)

πŸ“¦ Pro Tools for Dependency Management

Consider using:

  • pip-tools for smarter requirements.txt generation

  • Poetry or Pipenv for more structured dependency + environment management

  • Docker + requirements.txt for reproducible environments


🧠 Summary: Mastering pip and requirements.txt

CommandDescription
pip install packageInstall a package
pip freeze > requirements.txtSave current packages
pip install -r requirements.txtInstall packages from file
pip list --outdatedShow outdated packages
python -m venv venvCreate 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.


Read More 




Comments

Popular posts from this blog

Tosca System Requirements and Installation Guide (Step-by-Step)

How to Install Selenium for Python Step-by-Step

Tosca Commander: A Beginner’s Overview