How to load all modules in a folder?

后端 未结 18 1634
失恋的感觉
失恋的感觉 2020-11-22 05:37

Could someone provide me with a good way of importing a whole directory of modules?
I have a structure like this:

/Foo
    bar.py
    spam.py
    eggs.py         


        
相关标签:
18条回答
  • 2020-11-22 06:13

    I've created a module for that, which doesn't rely on __init__.py (or any other auxiliary file) and makes me type only the following two lines:

    import importdir
    importdir.do("Foo", globals())
    

    Feel free to re-use or contribute: http://gitlab.com/aurelien-lourot/importdir

    0 讨论(0)
  • 2020-11-22 06:13

    Look at the pkgutil module from the standard library. It will let you do exactly what you want as long as you have an __init__.py file in the directory. The __init__.py file can be empty.

    0 讨论(0)
  • 2020-11-22 06:15

    Just import them by importlib and add them to __all__ (add action is optional) in recurse in the __init__.py of package.

    /Foo
        bar.py
        spam.py
        eggs.py
        __init__.py
    
    # __init__.py
    import os
    import importlib
    pyfile_extes = ['py', ]
    __all__ = [importlib.import_module('.%s' % filename, __package__) for filename in [os.path.splitext(i)[0] for i in os.listdir(os.path.dirname(__file__)) if os.path.splitext(i)[1] in pyfile_extes] if not filename.startswith('__')]
    del os, importlib, pyfile_extes
    
    0 讨论(0)
  • 2020-11-22 06:16

    I know I'm updating a quite old post, and I tried using automodinit, but found out it's setup process is broken for python3. So, based on Luca's answer, I came up with a simpler answer - which might not work with .zip - to this issue, so I figured I should share it here:

    within the __init__.py module from yourpackage:

    #!/usr/bin/env python
    import os, pkgutil
    __all__ = list(module for _, module, _ in pkgutil.iter_modules([os.path.dirname(__file__)]))
    

    and within another package below yourpackage:

    from yourpackage import *
    

    Then you'll have all the modules that are placed within the package loaded, and if you write a new module, it'll be automagically imported as well. Of course, use that kind of things with care, with great powers comes great responsibilities.

    0 讨论(0)
  • 2020-11-22 06:16
    import pkgutil
    __path__ = pkgutil.extend_path(__path__, __name__)
    for imp, module, ispackage in pkgutil.walk_packages(path=__path__, prefix=__name__+'.'):
      __import__(module)
    
    0 讨论(0)
  • 2020-11-22 06:17

    Using importlib the only thing you've got to add is

    from importlib import import_module
    from pathlib import Path
    
    __all__ = [
        import_module(f".{f.stem}", __package__)
        for f in Path(__file__).parent.glob("*.py")
        if "__" not in f.stem
    ]
    del import_module, Path
    
    0 讨论(0)
提交回复
热议问题