Does Python requirements file have to specify version?

Question:

I have a requirements.txt file for a Python code base. The file has everything specified:

pytz==2017.2
requests==2.18.4
six==1.11.0

I am adding a new package. Should I list its version? If yes, how do I pick a version to specify?

Answers:

Check out the pip docs for more info, but basically you do not need to specify a version. Doing so can avoid headaches though, as specifying a version allows you to guarantee you do not end up in dependency hell.

Note that if you are creating a package to be deployed and pip-installed, you should use the install-requires metadata instead of relying on requirements.txt.

Also, it’s a good idea to get into the habit of using virtual environments to avoid dependency issues, especially when developing your own stuff. Anaconda offers a simple solution with the conda create command, and virtualenv works great with virtualenvwrapper for a lighter-weight solution. Another solution, pipenv, is quite popular.

Answered By: Engineero

No, there is no need to specify a version. It’s probably a good idea to specify one, though.

If you want to specify a version but you don’t know which version to specify, try using pip freeze, which will dump out a list of all the packages you currently have installed and what their versions are.

Answered By: Daniel Pryden

Specifying a version is not a requirement though it does help a lot in the future. Some versions of packages will not work well with other packages and their respective versions. It is hard to predict how changes in the future will effect these interrelationships. This is why it is very beneficial to create a snapshot in time (in your requirements.txt) showing which version interrelationships do work.

To create a requirements.txt file including the versions of the packages that you’re using do the following. In your console/ terminal cd into the location that you would like your requirement.txt to be and enter:

pip freeze > requirements.txt

This will automatically generate a requirement.txt file including the packages that you have installed with their respective versions.

A tip – you should aim to be using a virtual environment for each individual project that you’ll be working on. This creates a ‘bubble’ for you to work within and to install specific package versions in, without it effecting your other projects. It will save you a lot of headaches in the future as your packages and versions will be kept project specific. I suggest using Anacondas virtual environment.

Answered By: Toms Code
Categories: questions Tags: ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.