1

Lets say that I have a python package and I want to list out all the modules within this package. i.e.

>>> import my_package
>>> import inspect
>>> inspect.getmembers(my_package, inspect.ismodule)
>>> []

For some reason I am getting an empty list. Now if I manually load a module from the package then:

>>> import my_package.module_a
>>> inspect.getmembers(my_package, inspect.ismodule)
>>> ['module_a, <module '....'>]

Assuming that I have multiple modules and I don't want to load them one by one is there any other way to do it? (p.s. from my_package import * won't work).

Furthermore do you know what is causing this behavior? Is anything that has to do with namespaces?

P.S Packages are not locally installed, I am loading everything from a server.

1
  • What happens if you access my_package.module_a before the second import? Commented Jul 6, 2017 at 18:42

1 Answer 1

1

There isn't really a guaranteed way to get every possible module that could ever exist within a package namespace. You can search the package directory for subpackage directories and module files, but there's nothing stopping some bit of code from just creating a types.ModuleType object and attaching it the top package. If the package you're dealing with is a namespace package, there could be any number of modules and subpackages located in different locations on disk.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.