有人能为我提供一个导入整个模块目录的好方法吗? 我有一个这样的结构:

/Foo
    bar.py
    spam.py
    eggs.py

我尝试通过添加__init__.py并从Foo import *将其转换为一个包,但它没有按我希望的方式工作。


当前回答

这里有一个解决方案,您不必写文件名。只需将此代码片段添加到__init__.py中

from inspect import isclass
from pkgutil import iter_modules
from pathlib import Path
from importlib import import_module

# iterate through the modules in the current package
package_dir = Path(__file__).resolve().parent
for (_, module_name, _) in iter_modules([package_dir]):

    # import the module and iterate through its attributes
    module = import_module(f"{__name__}.{module_name}")
    for attribute_name in dir(module):
        attribute = getattr(module, attribute_name)

        if isclass(attribute):            
            # Add the class to this package's variables
            globals()[attribute_name] = attribute

其他回答

包含一个目录下的所有文件:

专为那些无法上手的新手准备的。

Make a folder /home/el/foo and make a file main.py under /home/el/foo Put this code in there: from hellokitty import * spam.spamfunc() ham.hamfunc() Make a directory /home/el/foo/hellokitty Make a file __init__.py under /home/el/foo/hellokitty and put this code in there: __all__ = ["spam", "ham"] Make two python files: spam.py and ham.py under /home/el/foo/hellokitty Define a function inside spam.py: def spamfunc(): print("Spammity spam") Define a function inside ham.py: def hamfunc(): print("Upgrade from baloney") Run it: el@apollo:/home/el/foo$ python main.py spammity spam Upgrade from baloney

扩展Mihail的回答,我认为非黑客的方式(即不直接处理文件路径)如下:

在Foo/下创建一个空的__init__.py文件 执行

import pkgutil
import sys


def load_all_modules_from_dir(dirname):
    for importer, package_name, _ in pkgutil.iter_modules([dirname]):
        full_package_name = '%s.%s' % (dirname, package_name)
        if full_package_name not in sys.modules:
            module = importer.find_module(package_name
                        ).load_module(full_package_name)
            print module


load_all_modules_from_dir('Foo')

你会得到:

<module 'Foo.bar' from '/home/.../Foo/bar.pyc'>
<module 'Foo.spam' from '/home/.../Foo/spam.pyc'>

我也遇到过这个问题,这是我的解决方案:

import os

def loadImports(path):
    files = os.listdir(path)
    imps = []

    for i in range(len(files)):
        name = files[i].split('.')
        if len(name) > 1:
            if name[1] == 'py' and name[0] != '__init__':
               name = name[0]
               imps.append(name)

    file = open(path+'__init__.py','w')

    toWrite = '__all__ = '+str(imps)

    file.write(toWrite)
    file.close()

这个函数创建一个名为__init__.py的文件(在提供的文件夹中),其中包含一个__all__变量,该变量保存文件夹中的每个模块。

例如,我有一个名为Test的文件夹 它包含:

Foo.py
Bar.py

所以在脚本中,我想把模块导入,我会写:

loadImports('Test/')
from Test import *

这将从Test中导入所有内容,Test中的__init__.py文件现在将包含:

__all__ = ['Foo','Bar']

Anurag Uniyal给出了改进建议!

#!/usr/bin/python
# -*- encoding: utf-8 -*-

import os
import glob

all_list = list()
for f in glob.glob(os.path.dirname(__file__)+"/*.py"):
    if os.path.isfile(f) and not os.path.basename(f).startswith('_'):
        all_list.append(os.path.basename(f)[:-3])

__all__ = all_list  

我已经为此创建了一个模块,它不依赖于__init__.py(或任何其他辅助文件),并让我只输入以下两行:

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

请随意重用或贡献:http://gitlab.com/aurelien-lourot/importdir