Roman Susi
Roman Susi

Reputation: 4199

How to compare requirement file and actually installed Python modules?

Given requirements.txt and a virtualenv environment, what is the best way to check from a script whether requirements are met and possibly provide details in case of mismatch?

Pip changes it's internal API with major releases, so I seen advices not to use it's parse_requirements method.

There is a way of pkg_resources.require(dependencies), but then how to parse requirements file with all it's fanciness, like github links, etc.?

This should be something pretty simple, but can't find any pointers.

UPDATE: programmatic solution is needed.

Upvotes: 12

Views: 11248

Answers (3)

Ilija
Ilija

Reputation: 131

No script option => If you use VS Code you can open both files in compare mode and check where they are different and where they are the same:

Step one: open files & folders side bar in VS Code and right click on file you would like to compare, click Select to Compare

enter image description here

Then you select second file, right click on Compare with Selected

enter image description here

And you will get something like this: black one are the same, red, green diffrent

enter image description here

Upvotes: 0

Fabien Snauwaert
Fabien Snauwaert

Reputation: 5661

Got tired of the discrepancies between requirements.txt and the actually installed packages (e.g. when deploying to Heroku, I'd often get ModuleNotFoundError for forgetting to add a module to requirements.)

This helps:

Use compare-requirements (GitHub)
(you'll need to pip install pipdeptree to use it.)

It's then as simple as...

cmpreqs --pipdeptree

...to show you (in "Input 2") which modules are installed, but missing from requirements.txt.

You can then examine the list and see which ones should in fact be added to requirements.txt.

Upvotes: 2

mohrtw
mohrtw

Reputation: 386

You can save your virtualenv's current installed packages with pip freeze to a file, say current.txt

pip freeze > current.txt

Then you can compare this to requirements.txt with difflib using a script like this:

import difflib

req = open('requirements.txt')
current = open('current.txt')

diff = difflib.ndiff(req.readlines(), current.readlines())
delta = ''.join([x for x in diff if x.startswith('-')])

print(delta)

This should display only the packages that are in 'requirements.txt' that aren't in 'current.txt'.

Upvotes: 13

Related Questions