Member-only story

Why and How to make a Requirements.txt

Using a Virtual Environment to Avoid Seeming like a Sadist

--

TL;DR: $ pip freeze > requirements.txt

Why not just write pretty code and push it to GitHub like a happy little clam, and not worry about making a requirements.txt? If my code runs on my computer, why should I give a care about my python environment? What even is a python environment? Perhaps a reticulated python’s terrarium?

There’s a happy little clam, in her environment

Nope. In short, we generate and share requirements.txt files to make it easier for other developers to install the correct versions of the required Python libraries (or “packages”) to run the Python code we’ve written.

Python Packages, and Environment

Open-source python packages — like beautifulsoup, or jupyter, or any of the other 158,872+ projects on the PyPi index — offer tremendous functionality, way beyond that of the standard Python library. It’s like you can push a button and download any one of a bazillion effects pedals for your neat but sort of vanilla Fender Stratocaster, for free:

Python + Open source packages = Fuego

When I say Python environment, I mean: The ecosystem consisting of your particular installed version of python, plus all the third-party packages (“libraries”) it can access (and their precise versions). Every time you $ pip installsomething, you are expanding your python environment, giving it access to packages that are not part of the Python standard library.

If you $ pip install a bunch of stuff outside of a virtual environment (more on this later), then you are adding to your “base” or “root” or “system” python environment. That is fine and good and totally valid for many sandbox-y purposes…

However, working exclusively in your base environment will inevitably cause headaches later, when you try to show the code you’ve built to other human beings. You can also start to run into compatibility issues (with your own code) as time goes on.

The problem we run into when we share our Python code is this: Not everyone has the same packages (and versions of those…

--

--

Robert Boscacci
Robert Boscacci

Written by Robert Boscacci

Get my book on computer vision and society for 99¢

Responses (17)

Write a response