How-to -- get a Python requirements.txt file

in «tip» by Michael Beard
Tags: , , , , , ,

Three basic questions for this:

  • what is a requirements.txt file
  • how do I generate/update a requirements.txt file?
  • how do I install the packages from the requirements.txt file?
What is a requirements.txt file?

The requirements.txt file is used for specifying what Python packages are required to be able to run your script/package. It basically lists all of the packages that you need to install before your script will run.

It is very much like a package.json file for JavaScript, if you are familar with that. Same concept. Or, if you're familar with Ruby, then it is like a Gemfile file. Again, like the package.json and requirements.txt files, the same concept.

It's a good thing to have, as it makes sharing your project with others much easier and makes building the project or running the script less of a pain.

How do I generate/update a requirements.txt file?

Generating a requirements.txt is pretty easy. You use pip and freeze the packages (or, to think of it slightly differently, you have pinned the versions of everything that you've installed since pip freeze had been run last - so, you probably will want to update the file after you've installed something or upgraded your environment).

Here is the basic instruction:

pip freeze > requirements.txt

That will generate/update the requirements.txt file for you.

How do I install the packages from the requirements.txt file?

Here is how you would install all the packages listed in the requirements.txt file:

pip install -r requirements.txt

pip will then install, using the versions listed in the requirements.txt file, so that you can have a repeatable build. Also, it can also play into the security of your project or script. Known version having bugs/defects or the like, can be easily checked to see if you have a security risk or not. Or, at the very least, you'll know when you have to upgrade to a new version of a package, either due to a bug or a fix to a bug!

Additional Info

Here are several sites that have some good information.

  • from pip documentation Requirements Files
    • may as well take it from the source itself. It gives examples for all actions.
    • There are other considerations here as well.
    • Overall, this is a good section to just read for general information.
  • How to install Python packages wit pip and requirements.txt - doesn't say anything different than the other above, but shows them explicitily
  • How to Update Requirements Files - this is specific for the PyNWB project, but it is applicable for any project.
    • It also shows that you could have a requirements.txt, requirements-dev.txt, and/or a requirements-doc.txt file as well.
    • It also has a couple of scripts that look interesting as well, that could easily be adapted to any project
  • Building a Python requirements.txt File - has an interesting way to find out what versions a package supports
  • for some historical perspective - Why and How to make a Requirements.txt - but it does have some good information regardless