Skip to content

Instantly share code, notes, and snippets.

@thevickypedia
Created December 2, 2023 18:29
Show Gist options
  • Save thevickypedia/1a918c47061d7fc08228c080c75cfcc4 to your computer and use it in GitHub Desktop.
Save thevickypedia/1a918c47061d7fc08228c080c75cfcc4 to your computer and use it in GitHub Desktop.
Update requirements.txt to what's currently available in pip freeze
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