Course contentsShow
← Python
Lesson 503 of 1,0608. Professional Tooling and PackagingFree lesson

Managing Project Dependencies with Pip

Learn to install, upgrade, and remove external Python packages using the standard pip package manager within your local development environment.

In this lesson, you will learn how to maintain your project’s external code requirements using pip, the standard package manager for Python.

Now that you are familiar with isolated environments, you need a way to populate those environments with the specific libraries your project requires. pip interfaces directly with the Python Package Index (PyPI) to download, track, and remove these dependencies.

Installing and Upgrading Packages

When you need an external library, such as requests, you install it using the command line within your activated virtual environment:

pip install requests

If you discover that a library has released a critical security patch or a new feature, you can bring your local version up to date with the --upgrade flag:

pip install --upgrade requests

Removing Unused Dependencies

As your project evolves, you may find that certain libraries are no longer necessary. Leaving unused packages in your environment increases the risk of dependency conflicts and bloats your deployment size. You can remove a package cleanly using:

pip uninstall requests

Managing Versions

To ensure your project behaves consistently across different development machines, you should always be explicit about versions. You can install a specific version to avoid breaking changes:

pip install requests==2.31.0

Checking Your Environment

It is good practice to periodically review what is installed in your current environment to ensure you aren't carrying "polluting" packages that aren't actually part of your project’s requirements:

pip list

This command displays every package installed and its corresponding version. If you see items here that aren't defined in your requirements.txt file (which you learned to manage in previous lessons), consider uninstalling them to keep your workspace pristine and predictable. By mastering these basic pip commands, you ensure that your local development environment remains a precise reflection of your project’s needs.

Key Takeaway: Use pip install, pip uninstall, and pip list to maintain a clean, version-controlled environment that contains only the specific dependencies required for your project to function.