Python is an easy way to read all import statements from a py module

I am trying to create a helper function to read a file and mock import for unit test. I have to read the vs import file since I don't have these things in the python path.

Code example:


#module.py import com.stackoverflow.question from com.stackoverflow.util import test_func from com.stackoverflow.util import TestClass 

 #magic helper: what i want magic = process('<path_to>/module.py') for module in magic.modules_as_strings(): #todo would have to recuirsively add each path # so i would first create com, then com.stackoverflow, etc setattr(self, module, StubModules(module) for obj in magic.sink: #these would be "from" from x import Y #its basically just creating self.Y = object setattr(self, object) 

Above is the code layout, I'm really looking for the best way to just curse the file for "statements / imports"

It makes sense? I know that I can read the file line by line, but I was hoping for a cleaner / more concise way.

Let me know if you have any questions.

+4
source share
1 answer

Using the AST module, this is pretty simple:

 import ast from collections import namedtuple Import = namedtuple("Import", ["module", "name", "alias"]) def get_imports(path): with open(path) as fh: root = ast.parse(fh.read(), path) for node in ast.iter_child_nodes(root): if isinstance(node, ast.Import): module = [] elif isinstance(node, ast.ImportFrom): module = node.module.split('.') else: continue for n in node.names: yield Import(module, n.name.split('.'), n.asname) 

For a module like this:

 from coco import bunny from coco.bungy import carrot from meta import teta from rocket import spaceship as sp import bingo import com.stackoverflow import motorbike as car import module1, module2 s="a random variable" def func(): """And a function""" 

Output:

 >>> for imp in get_imports("/path/to/file.py"): print imp Import(module=['coco'], name=['bunny'], alias=None) Import(module=['coco', 'bungy'], name=['carrot'], alias=None) Import(module=['meta'], name=['teta'], alias=None) Import(module=['rocket'], name=['spaceship'], alias='sp') Import(module=[], name=['bingo'], alias=None) Import(module=[], name=['com', 'stackoverflow'], alias=None) Import(module=[], name=['motorbike'], alias='car') Import(module=[], name=['module1'], alias=None) Import(module=[], name=['module2'], alias=None) 
+9
source

Source: https://habr.com/ru/post/1393021/


All Articles