Skip to content

Instantly share code, notes, and snippets.

@zulzeen
Last active November 23, 2018 10:30
Show Gist options
  • Save zulzeen/5da7b7b13a961e894e5c401a4832acea to your computer and use it in GitHub Desktop.
Save zulzeen/5da7b7b13a961e894e5c401a4832acea to your computer and use it in GitHub Desktop.
List all uninstalled import and add them to a requirement.txt file
import os
import importlib
all_imports = set()
for file in [filename for filename in os.listdir(os.path.curdir) if filename.endswith('.py')]:
with open(file, 'r') as current_file:
for line in current_file.readlines():
if line.startswith('import') or line.startswith('from'):
all_imports.add(line.split()[1])
with open('requirements.txt', 'a') as requirement_txt:
for i in all_imports:
try:
_ = importlib.import_module(i)
except ModuleNotFoundError:
requirement_txt.write(f'{i}\n')
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment