Python Requirements.txt

This is easily found elsewhere on the web, but I did it wrong the first time. So, note to self!

The command to create a requirements.txt file is:

$ pip freeze > requirements.txt

The catch is... first, navigate to the folder where you want the file to be created. Second, activate what ever environment you are using. In my case, I'm using conda on linux: $ source activate myenv

If you don't have the desired environment active, the command creates the requirements.txt file based on your default python installation.

The other side of the coin is when you are moving your python project to another computer and you need to install the same packages.

  • Copy the files and folders for the project to the desired location.
  • If you don't already have your environment set, do so.
  • Use the following command to install all the requirements

$ pip install -r requirements.txt

So, lessons learned were:

  • activate your environment first.
  • Also learned that pyCharm will volunteer to install the packages listed in requirements.txt for you. That's a nice feature.

Additional References

I obviously give no details about python virtual environments in the post. When I first started out with python, I didn't need them. This was because I was mainly using import from the standard library and the few 3rd party packages that I used were pretty standard so I didn't mind them being in my base python installation. But, as I began to create more projects with different requirements and wildly different packages, it becomes an obvious solution. I typically use "conda" these days, but virtualenv is a lighter weight method to get started with environments.

So, if you are a beginner who hasn't dabbled with virtual environments yet, be sure to check those out. A good introductory article is Python Virtual Environments - a Primer

Written by shakiestnerd in Technology on Wed 16 August 2017.

2018 by ShakiestNerd