有人能为我提供一个导入整个模块目录的好方法吗? 我有一个这样的结构:
/Foo
bar.py
spam.py
eggs.py
我尝试通过添加__init__.py并从Foo import *将其转换为一个包,但它没有按我希望的方式工作。
有人能为我提供一个导入整个模块目录的好方法吗? 我有一个这样的结构:
/Foo
bar.py
spam.py
eggs.py
我尝试通过添加__init__.py并从Foo import *将其转换为一个包,但它没有按我希望的方式工作。
当前回答
我知道我正在更新一个相当旧的帖子,我尝试使用automodinit,但发现它的设置过程对python3是坏的。所以,基于Luca的回答,我想出了一个更简单的答案——可能不适合。zip——来解决这个问题,所以我想我应该在这里分享它:
在你的包的__init__.py模块中:
#!/usr/bin/env python
import os, pkgutil
__all__ = list(module for _, module, _ in pkgutil.iter_modules([os.path.dirname(__file__)]))
在你的包下面的另一个包里:
from yourpackage import *
然后,您将加载包中放置的所有模块,如果您编写了一个新模块,它也将被自动导入。当然,要小心使用这些东西,能力越大责任越大。
其他回答
将__all__变量添加到__init__.py,包含:
__all__ = ["bar", "spam", "eggs"]
参见http://docs.python.org/tutorial/modules.html
注意你的__init__.py定义了__all__。模块-包文档说
The __init__.py files are required to make Python treat the directories as containing packages; this is done to prevent directories with a common name, such as string, from unintentionally hiding valid modules that occur later on the module search path. In the simplest case, __init__.py can just be an empty file, but it can also execute initialization code for the package or set the __all__ variable, described later. ... 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.
我也遇到过这个问题,这是我的解决方案:
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']
扩展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'>
我想补充Anurag Uniyal的回答。 你可以让它变得更简单,去掉大量的导入。 __init__.py文件的内容:
from os import listdir
from os.path import dirname
__all__ = [i[:-3] for i in listdir(dirname(__file__)) if not i.startswith('__') and i.endswith('.py')]