4

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?

VeLKerr
  • 2,995
  • 3
  • 24
  • 47
  • http://stackoverflow.com/questions/139180/listing-all-functions-in-a-python-module , http://stackoverflow.com/questions/4040620/is-it-possible-to-list-all-functions-in-a-module ... ? – rbaleksandar Feb 08 '16 at 16:14
  • @rbaleksandar, I saw these posts where tried to find solution. But non of them aren't working for me (see my edits). – VeLKerr Feb 08 '16 at 16:39
  • This question **isn't duplicate** because all of answers posted [here](http://stackoverflow.com/questions/4040620/is-it-possible-to-list-all-functions-in-a-module) aren't works for me (at first, I have a **multimodule** package, at second, I need to get **function objects**, not only names). – VeLKerr Feb 08 '16 at 16:47
  • [This answer](http://stackoverflow.com/a/4040709/1559401) states: "`all_functions` will be a list of tuples where the first element is the name of the function and the second element is the function itself." – rbaleksandar Feb 08 '16 at 17:01
  • You might be able to do something like what is shown in [this answer](http://stackoverflow.com/a/14428820/355230) which dynamically imports all the modules in the package — change the part near the end that adds objects to the global namespace to iterate through what it finds in each sub-module looking for function objects. – martineau Feb 08 '16 at 17:02
  • @rbaleksandar, ok, but it works only for module, not for package (see part of post, starts with *When...* and my edit num. 2). – VeLKerr Feb 08 '16 at 17:04
  • @VeLKerr `dir` works for me. You are definitely doing something wrong. Can you post fully reproducible code? – Andrey Feb 08 '16 at 17:13
  • @Andrey, 1st code snippet in my post gives me the list of `str`s. So, `dir()` gives list of names available for `str` type. If `dir()` works for your *multimodule* package, how did you get `module`-objects for package? – VeLKerr Feb 08 '16 at 19:34

1 Answers1

0

try this:

for importer, module, ispkg in pkgutil.iter_modules(package.__path__):
   print dir(__import__(module))
Andrey
  • 59,039
  • 12
  • 119
  • 163
  • Thanks, but it isn't working. I can't import submodule without parent module, so if I run your snippet, I get: `ImportError: No module named sub_module`. I solved this as following: `print dir(__import__(package.__name__ + '.' + module))`. But again, I get a list of `str`s, but need the `module` objects or some similar objects, from which I'll get functions. – VeLKerr Feb 08 '16 at 21:05
  • @VeLKerr then load parent module(s) first. – Andrey Feb 08 '16 at 21:36