简体   繁体   English

如何使子模块的类在父模块的命名空间中可用?

[英]How do I make classes from a submodule available in a parent module's namespace?

Here's a python module. 这是一个python模块。 foo is in sys.path . foosys.path

foo\
    __init__.py
    bar\
        __init__.py
        base.py
            class Base(object)
        derived.py
            import foo.bar.base as base
            class Derived(base.Base)

I've got nothing fancy going on yet. 我还没有幻想。 If I want to instantiate the Derived class from the derived module, I can do that easily enough: 如果我想从实例派生derived模块,我能做到这一点很容易不够:

import foo.bar.derived as derived
print(derived.Derived())

However, I'd like to just import the bar module and call bar.Derived() , because I plan to have lots of classes within lots of different modules, and I don't want to deal with all these tentacular import paths. 但是,我只想导入bar模块并调用bar.Derived() ,因为我计划在许多不同的模块中包含许多类,并且我不想处理所有这些触目惊心的导入路径。 My understanding is that I can simply import Derived into the namespace of the bar module, by modifying my project like so: 我的理解是,可以像这样修改项目,将Derived导入到bar模块的命名空间中:

foo\
    __init__.py
    bar\
        __init__.py
            from foo.bar.derived import Derived
        base.py
            class Base(object)
        derived.py
            import foo.bar.base as base
            class Derived(base.Base)

Now I should be able to do the following: 现在,我应该能够执行以下操作:

import foo.bar as bar
print(bar.Derived())

But I get an AttributeError complaining that the foo module has no submodule called bar : 但是我收到AttributeError抱怨说foo模块没有名为bar子模块:

test.py             (1): import foo.bar
foo\bar\__init__.py (1): from foo.bar.derived import Derived
foo\bar\derived.py  (1): import foo.bar.base as base

AttributeError: 'module' object has no attribute 'bar'

In fact, my original test code (at top) doesn't work either! 实际上,我的原始测试代码(顶部)也不起作用! As soon as I try to import foo.bar , I get errors. 尝试导入foo.bar ,我立即收到错误消息。

What I can gleam from this error is that the import statement in __init__.py causes derived.py to be executed before bar is fully loaded, and therefore it can't import the module (also from bar ) which contains its own base class. 我可以从该错误中看到的是__init__.py中的import语句导致在bar完全加载之前执行了derived.py ,因此它无法(也从bar )导入包含其自身基类的模块。 I'm coming from the C++ world, where ultra-nested namespaces aren't as integral and a simple forward declaration would negate this problem, but I've been led to believe that what I'm looking for is possible and at least a somewhat acceptably Pythonic solution. 我来自C ++世界,那里的超嵌套命名空间并不那么完整,简单的前向声明可以解决这个问题,但是我一直被认为我正在寻找的东西是可能的,至少有点可接受的Pythonic解决方案。 What am I doing wrong? 我究竟做错了什么? What's the correct way to make classes from a submodule available in the parent module's namespace? 使子模块中的类在父模块的命名空间中可用的正确方法是什么?

If you're working with Python 2.5 or later, try using explicit relative imports ( http://www.python.org/dev/peps/pep-0328/#guido-s-decision ): 如果您使用的是Python 2.5或更高版本,请尝试使用显式相对导入( http://www.python.org/dev/peps/pep-0328/#guido-s-decision ):

test.py             (1): import foo.bar
foo\bar\__init__.py (1): from .derived import Derived
foo\bar\derived.py  (1): from . import base

(Note that if you are indeed working with Python 2.5 or 2.6, you'll need to include from __future__ import absolute_import in your modules.) (请注意,如果您确实在使用Python 2.5或2.6,则需要在模块中包含from __future__ import absolute_import 。)

in derived.py, use this: 在derived.py中,使用以下命令:

EDIT : as JAB pointed out, implicit relative imports are deprecated , to the following isn't recommended (although it does work still in Python 2.7 - with no deprecation errors!). 编辑 :正如JAB所指出的那样,不建议使用隐式相对导入,建议不要使用以下内容(尽管它在Python 2.7中仍然可以使用-没有使用错误!)。

import base # this is all you need - it's in the current directory import base#这就是您所需要的-它在当前目录中

Instead, use: 而是使用:

from . import base # 

(or) (要么)

from foo.bar import base

instead of: 代替:

import foo.bar.base as base

This will solve both your errors (since they're from the same issue). 这将解决您的两个错误(因为它们来自同一问题)。 Your import doesn't work since there is no base function or class inside the foo.bar.base module. 您的导入无法正常进行,因为foo.bar.base模块中没有base函数或类。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 在python中,如何通过父模块或顶级模块访问子类的实例以及子模块 - In python how do I access an instance of a class from a parent or top-level module from withing a submodule 在python中,如何在不保留导入模块名称空间的情况下从其他模块导入所有类? - In python, how do you import all classes from another module without keeping the imported module's namespace? 如何在 Go 中使用 Python 模块中的变量? - How do I make variables from a Python module available in Go? 与__init__.py混淆并从子模块导入父模块中的类 - Confusion with __init__.py and importing classes in parent module from submodule 如何将子模块导入父名称空间? - How to import submodule into parent namespace? 从子模块导入父模块 - Import parent module from submodule 如何在全局名称空间的模块中创建变量? - How do I make a variable in a module on a global namespace? 如何使用__all__阻止用户在子模块中显示x时从子模块中导入x - How do I prevent users from importing x from a submodule when it exposed in the parent using __all__ Python-如何从扩展名称空间导入父包 - Python - How do I import parent package from extended namespace 如何在Python中将子模块调用重定向到父模块 - How to redirect submodule calls to parent module in Python
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM