Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

List all third party packages and theirselves functions used in a Python file

Tags:

python

I have many python packages, wrote 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 like this

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 that, all I can come up implementation of this tool is read a Python file line by one line as string, and use regex to compare it. Could you give me some advises?

If you don't know what I mean, please comment your question, I will fix it as soon as possible. Thanks!

like image 482
Isaac Avatar asked Jan 20 '16 12:01

Isaac


People also ask

What are third party packages in Python?

A third party module is any code that has been written by a third party (neither you nor the python writers (PSF)).

What are the modules and packages in Python?

The module is a simple Python file that contains collections of functions and global variables and with having a . py extension file. It is an executable file and to organize all the modules we have the concept called Package in Python.


1 Answers

You can parse your files with the ast module and check all Import and ImportFrom statements.

To give you an idea, here's 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 could work like this:

  1. Parse the source file via ast.parse
  2. Visit each node using ast.walk()
  3. If a node is an Import or ImportFrom object, then inspect the names and do what you have to do.

Using ast is way much easier and more robust than regular expressions or a custom parser.

like image 184
Andrea Corbellini Avatar answered Nov 10 '22 00:11

Andrea Corbellini