Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- # autoimport.py
- import ast
- import importlib
- import sys
- def analyze_script(script_path):
- with open(script_path, 'r') as file:
- tree = ast.parse(file.read(), filename=script_path)
- imports = set()
- for node in ast.walk(tree):
- if isinstance(node, ast.Import):
- for alias in node.names:
- imports.add(alias.name)
- elif isinstance(node, ast.ImportFrom):
- module_name = node.module
- if module_name:
- imports.add(module_name)
- return imports
- def execute_imports(imports):
- for module_name in imports:
- try:
- importlib.import_module(module_name)
- print(f"Imported: {module_name}")
- except ImportError as e:
- print(f"Failed to import {module_name}: {e}")
- def main(script_path):
- imports = analyze_script(script_path)
- execute_imports(imports)
- if __name__ == "__main__":
- if len(sys.argv) != 2:
- print("Usage: python autoimport.py <path_to_main_script>")
- sys.exit(1)
- main(sys.argv[1])
- '''
- import autoimport
- def calculate_area(radius):
- return math.pi * radius ** 2
- print(calculate_area(5))
- '''
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement