4

Is it possible in python to get a list of modules from a folder/package and import them?

I would like to be able to do this from a function inside a class, so that the entire class has access to them (possibly done from the __init__ method).

Any help would be greatly appreciated.

4
  • Are from package import * and dir(package) insufficient? Note that dir is a Python command, as well as a command line one. Commented Oct 21, 2011 at 15:02
  • @Hannele Does that really work for you? I get ['__builtins__', '__doc__', '__file__', '__name__', '__package__', '__path__'] without any mention of the modules in package. dir shows the modules if the modules themselves are explicitly imported, though, but that defeats the purpose of the question. Commented Oct 21, 2011 at 15:14
  • I might have to double-check how that works with a folder - do you have an __init__.py file in the folder? Even empty, this is what tells python to treat the folder as a module. Commented Oct 21, 2011 at 15:23
  • Yes, I had an __init__.py. What I didn't have is what @MarkHildreth helpfully referenced: the line __all__ = ['bar','baz'] in __init__.py. Commented Oct 21, 2011 at 15:36

3 Answers 3

4

See the modules document.

The only solution is for the package author to provide an explicit index of the package. The import statement uses the following convention: if a package’s __init__.py code defines a list named __all__, it is taken to be the list of module names that should be imported when from package import * is encountered. It is up to the package author to keep this list up-to-date when a new version of the package is released. Package authors may also decide not to support it, if they don’t see a use for importing * from their package. For example, the file sounds/effects/__init__.py could contain the following code:

__all__ = ["echo", "surround", "reverse"] 

This would mean that from sound.effects import * would import the three named submodules of the sound package.

Yes, you could find a way to do this by doing a directory listing for the files in the directory and import them manually. But there isn't built-in syntax for what you're asking.

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

Comments

0

You can know the list of the modules with the dir function

import module dir (module) 

Later in a program, you can import a single function :

from module import function 

3 Comments

When I try dir(foo) (with foo being a package) I get ['__builtins__', '__doc__', '__file__', '__name__', '__package__', '__path__'] which doesn't contain a list of the modules in the package.
@EricWilson >>> type(dir(sys)) <type 'list'>
This won't get you the list of modules. This will get you the list of attributes for the package (as defined in the __init__.py file of the package's directory). The modules for the package are only included in this list if they are explicitly stated in the __all__ variable.
0

The distribute module provides a mechanism that does much of this. First, you might start by listing the python files in a package using pkg_resources.resource_listdir:

>>> module_names = set(os.path.splitext(r)[0] ... for r ... in pkg_resources.resource_listdir("sqlalchemy", "/") ... if os.path.splitext(r)[1] in ('.py', '.pyc', '.pyo', '') ... ) - set(('__init__',)) >>> module_names set(['engine', 'util', 'exc', 'pool', 'processors', 'interfaces', 'databases', 'ext', 'topological', 'queue', 'test', 'connectors', 'orm', 'log', 'dialects', 'sql', 'types', 'schema']) 

You could then import each module in a loop:

modules = {} for module in module_names: modules[module] = __import__('.'.join('sqlalchemy', module)) 

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.