Python Friday #21: Manage Your Dependencies

The bigger your project grows, the more dependencies it has. Python offers a simple way to capture them and let everyone on your team install the exact same versions.

This post is part of my journey to learn Python. You can find the other parts of this series here.

 

Creating a package list

The command pip freeze shows you a list of all installed packages and their version. You can store this output in a file called requirements.txt and put it into version control:

You can check what list this command returns by calling pip freeze without pushing the output into the file:

 

Installing the packages from that list

If you ship a requirements.txt with your project, your users can use the -r option and install the necessary dependencies from that file in their environment:

 

More flexibility

If you use pip freeze you get a list of the exact version number of your dependencies. However, sometimes your program works with more versions than just this specific one. In this case you have multiple options to be less specific about your dependencies. The example requirements file in the documentation of pip has all the possibilities covered:

For even more details you can read PEP 440.

 

Conclusion

The requirements.txt is a simple way to track the dependencies of your project and share them with the users.

1 thought on “Python Friday #21: Manage Your Dependencies”

Leave a Comment

This site uses Akismet to reduce spam. Learn how your comment data is processed.