VeLKerr February 2016

Getting all functions of python package

I have a multimodule Python package which has one file with class MyClass and few files with functions (one file can contain more than 1 function).

I tried to get all functions in this package.

Firstly, using the following:

modules = []
for importer, module, ispkg in pkgutil.iter_modules(package.__path__):
    modules.extend(module)

but I got only module names, not module objects.

When I tried to get functions of these modules using inspect.getmembers(module, inspect.isfunction), I of course, got an empty collection.

So, how can I got all module objects from the package for further getting functions?

Does an easier way to get all functions from the multifile package exists?

P.S. I need exactly function objects and not only their names.

Edit.

  1. dir(package) gives me only built-in variables:

    ['__builtins__', '__doc__', '__file__', '__name__', '__package__', '__path__']

  2. inspect.getmembers(package, inspect.infunction) gives an empty list as well as the same code for each module as I described above.

  3. dir(module) gives me the name list of all methods available for str objects (because, iter_modules() gives only names of modules).

  4. AST... Are you sure, that there isn't simpler solutions?

Answers


Andrey February 2016

try this:

for importer, module, ispkg in pkgutil.iter_modules(package.__path__):
   print dir(__import__(module))

Post Status

Asked in February 2016
Viewed 3,381 times
Voted 13
Answered 1 times

Search




Leave an answer