English 中文(简体)
如何导入 Python 命名空间软件包的所有子模块?
原标题:How do I import all the submodules of a Python namespace package?

Python 命名空间软件包可以分散在许多目录和 zip 文件或自定义导入器中。 在名称空间软件包的所有可导入子模块中循环的正确方式是什么?

问题回答

以下是一个对我有效的方法。 创建一个新的子模块 < code>all. py , 例如, 在命名空间中的一个软件包中 。

如果您写了

import mynamespace.all

您被给定了 mynamespace 模块的对象。 此对象包含名称空间中所有已装入的模块, 无论它们在哪里被装入, 因为周围只有一个 mynamespace 实例。

所以,只需在命名空间中以 all.py 格式装入所有包!

# all.py

from pkgutil import iter_modules

# import this module s namespace (= parent) package
pkg = __import__(__package__)

# iterate all modules in pkg s paths,
# prefixing the returned module names with namespace-dot,
# and import the modules by name
for m in iter_modules(pkg.__path__, __package__ +  . ):
    __import__(m.name)

或者在让 all 模块保持空白的单行线中,如果你关心的是这种事情:

# all.py

(lambda: [__import__(_.name) for _ in __import__( pkgutil ).iter_modules(__import__(__package__).__path__, __package__ +  . )])()  # noqa

从您的命名空间导入 all 模块后, 您实际上会收到一个满载的命名空间模块 :

import mynamespace.all

mynamespace.mymodule1  # works
mynamespace.mymodule2  # works
...

当然,如果您不想立即导入,您可以使用相同的机制在命名空间中列出模块或以其他方式处理模块。

请阅读import conflution

它非常明确地区分了您可以导入软件包及其子模块的所有不同方式,并在过程中解答您的问题。当您需要从软件包中找到某个子模块时,通常比 import io.drivers.zip 更方便地从 o.drivers 导入 zip < /code > 中写入 。 因为前者只允许您将模块称为 zip 而不是 整个名称 。

from modname inport /code>, 这是将模块中的所有项目导入到当前命名空间的简单方式; 但是, 此语句应该少用一点 。





相关问题
Can Django models use MySQL functions?

Is there a way to force Django models to pass a field to a MySQL function every time the model data is read or loaded? To clarify what I mean in SQL, I want the Django model to produce something like ...

An enterprise scheduler for python (like quartz)

I am looking for an enterprise tasks scheduler for python, like quartz is for Java. Requirements: Persistent: if the process restarts or the machine restarts, then all the jobs must stay there and ...

How to remove unique, then duplicate dictionaries in a list?

Given the following list that contains some duplicate and some unique dictionaries, what is the best method to remove unique dictionaries first, then reduce the duplicate dictionaries to single ...

What is suggested seed value to use with random.seed()?

Simple enough question: I m using python random module to generate random integers. I want to know what is the suggested value to use with the random.seed() function? Currently I am letting this ...

How can I make the PyDev editor selectively ignore errors?

I m using PyDev under Eclipse to write some Jython code. I ve got numerous instances where I need to do something like this: import com.work.project.component.client.Interface.ISubInterface as ...

How do I profile `paster serve` s startup time?

Python s paster serve app.ini is taking longer than I would like to be ready for the first request. I know how to profile requests with middleware, but how do I profile the initialization time? I ...

Pragmatically adding give-aways/freebies to an online store

Our business currently has an online store and recently we ve been offering free specials to our customers. Right now, we simply display the special and give the buyer a notice stating we will add the ...

Converting Dictionary to List? [duplicate]

I m trying to convert a Python dictionary into a Python list, in order to perform some calculations. #My dictionary dict = {} dict[ Capital ]="London" dict[ Food ]="Fish&Chips" dict[ 2012 ]="...