Created
December 6, 2021 15:33
-
-
Save JoaoG250/840abacb92b547b2912ba7f7642c0c2d to your computer and use it in GitHub Desktop.
Python script that checks for differences between virtualenv and requirements.txt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import subprocess | |
def make_reqs_dict(req_list): | |
reqs = {} | |
for req in req_list: | |
if req != "" and "git+" not in req: | |
req_name, req_version = req.split("==") | |
reqs[req_name] = req_version | |
return reqs | |
# Get list of packages installed in virtualenv | |
result = subprocess.run(["pip", "freeze"], stdout=subprocess.PIPE) | |
freeze = result.stdout.decode("utf-8") | |
env_reqs_list = freeze.split("\n") | |
# Make a dictionary of packages and versions | |
env_reqs = make_reqs_dict(env_reqs_list) | |
# Read the requirements.txt file | |
file_reqs_list = [] | |
with open("requirements.txt") as f: | |
for line in f: | |
file_reqs_list.append(line.strip()) | |
# Make a dictionary of packages and versions | |
file_reqs = make_reqs_dict(file_reqs_list) | |
# Compare the two dictionaries and print out any differences | |
for req in file_reqs: | |
if req not in env_reqs: | |
print("{} is not installed in the virtualenv".format(req)) | |
elif file_reqs[req] != env_reqs[req]: | |
print( | |
"{} version mismatch: {} in virtualenv, {} in requirements.txt".format( | |
req, env_reqs[req], file_reqs[req] | |
) | |
) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment