List of all third-party packages and their functions used in the Python file

I have many python packages written by my colleagues, and I want to write a tool to check which third packages they rely on.

Like this

#it is my package, need to check,call it example.py #We have more than one way to import a package, It is a problem need to consider too from third_party_packages import third_party_function def my_function(arg): return third_party_function(arg) 

and the tool should work as follows

 result = tool(example.py) #this result should be a dict like this structure #{"third_party_function":["my_function",]} #Means "my_function" relies on "third_party_function" 

I have no idea how to do this, all I can think of for implementing this tool is reading a line of a Python file as one line as a line and using regex to compare it. Could you give me some advice?

If you do not know what I mean, comment on your question, I will fix it as soon as possible. Thanks!

+6
source share
1 answer

You can analyze your files with ast and check all Import and ImportFrom .

To give you an idea, here is an example:

 >>> import ast >>> tree = ast.parse('import a; from b import c') >>> tree.body [<_ast.Import object at 0x7f3041263860>, <_ast.ImportFrom object at 0x7f3041262c18>] >>> tree.body[0].names[0].name 'a' >>> tree.body[1].module 'b' >>> tree.body[1].names[0].name 'c' 

Your script may work as follows:

  • Parse the source file with ast.parse
  • Visit each node using ast.walk()
  • If the node is an Import or ImportFrom , then check the names and do what you need to do.

Using ast much simpler and more reliable than regular expressions or a custom parser.

+3
source

All Articles