Python Friday #55: Separate Development From Runtime Dependencies

We need pytest and other dependencies only while we develop our application, but not when we run them in production. Is there a way that allows us to track the dependencies but not install all of them all the time?

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

 

Everything in the requirements.txt

I wrote in Python Friday #21 about the requirements.txt file and how it helps to manage the dependencies of your application. This mechanism is a great help to install all the needed dependencies in one go – and it is the recommended way.

 

A separate file for development requirements

I got this trick from the course Building Data-Driven Web Apps with Flask and SQLAlchemy in which Michael Kennedy explains that we can reference other files inside a requirements.txt file. This allow us to create a requirements-dev.txt, add all the development-only dependencies and reference our regular requirements.txt like this:

It is possible to list packages in both files. But this is a source of future errors and we better remove the no longer needed packages from requirements.txt right now:

 

Change in your process

On your production server, you install the dependencies as before:

On your development machine, you use your new file instead:

 

Conclusion

This little trick allows us to reduce the number of dependencies we need to install on our production server while we continue to follow the recommended way to handle dependencies. I like this approach a lot and can recommend it.

1 thought on “Python Friday #55: Separate Development From Runtime Dependencies”

Leave a Comment

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