Created
December 2, 2023 18:29
-
-
Save thevickypedia/1a918c47061d7fc08228c080c75cfcc4 to your computer and use it in GitHub Desktop.
Update requirements.txt to what's currently available in pip freeze
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 os | |
import shutil | |
import subprocess | |
import time | |
with open('requirements.txt') as file: | |
requirements = file.readlines() | |
tmp_name = f'requirements_{int(time.time())}.txt' | |
os.rename('requirements.txt', tmp_name) | |
with open('requirements.txt', 'a') as file: | |
for requirement in requirements: | |
file.write( | |
subprocess.check_output( | |
f'{shutil.which("python")} -m pip freeze | grep {requirement.strip().replace("_", "-")}', | |
shell=True, stderr=subprocess.DEVNULL).decode() | |
) | |
os.remove(tmp_name) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment