Created
August 6, 2024 08:20
-
-
Save yezz123/d4c959463ee2c7a19f05945377201da7 to your computer and use it in GitHub Desktop.
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 ast | |
def extract_imports(file_path): | |
with open(file_path, 'r') as file: | |
tree = ast.parse(file.read()) | |
imports = [] | |
for node in ast.walk(tree): | |
if isinstance(node, ast.Import): | |
for alias in node.names: | |
imports.append(alias.name) | |
elif isinstance(node, ast.ImportFrom): | |
module = node.module | |
for alias in node.names: | |
imports.append(f"{module}.{alias.name}") | |
return imports | |
def get_python_files(directory): | |
for root, _, files in os.walk(directory): | |
for file in files: | |
if file.endswith('.py'): | |
yield os.path.join(root, file) | |
def main(project_directory): | |
all_imports = set() | |
for file_path in get_python_files(project_directory): | |
file_imports = extract_imports(file_path) | |
all_imports.update(file_imports) | |
return sorted(all_imports) | |
if __name__ == "__main__": | |
project_dir = input("Enter the path to your Python project: ") | |
imports = main(project_dir) | |
print("All imports used in the project:") | |
for imp in imports: | |
print(imp) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment