Last active
November 23, 2018 10:30
-
-
Save zulzeen/5da7b7b13a961e894e5c401a4832acea to your computer and use it in GitHub Desktop.
List all uninstalled import and add them to a requirement.txt file
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 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