简体   繁体   English

如何使当前模块导入__init__.py文件?

[英]How to get current module being imported in the __init__.py file?

package
    1) module1
    2) module2
    3) module3
    4) __init__.py

what I want to do is 我想做的是

  1. import package 进口package
  2. then I could use some sub modules in module1 , module2 directly, just looks like if I have imported it in the following way: 那么我可以直接在module1module2使用一些子模块,就像我以下列方式导入它一样:

     import module1 

If I use imp.load_module in __init__.py to load module1 , then dir(package1) will have "module1" , "module2" in sys.modules , but I still need to use package.modules1 to access it. 如果我在__init__.py使用imp.load_module加载module1 ,则dir(package1)sys.modules中将具有"module1""module2" ,但是我仍然需要使用package.modules1来访问它。 I noticed that when I import package , I will get a 'module' object package. 我注意到,当我导入package ,我会得到一个'module'对象包。 If I update the package's built-in method, it will trick the system to make module1 visible. 如果我更新了程序包的内置方法,它将欺骗系统以使module1可见。

>>> import package
>>> package.__dict__.update(package.module1.__dict__)

then everything looks fine and it seems to python the module1 has been imported like import module1 and I could use class or modules in module1 directly. 然后一切都看起来不错,它似乎对Python的module1已经进口同类import module1 ,我可以在使用类或模块module1直接。

Then comes up my question --- How to detect currently module object in __init__.py ? 然后出现我的问题---如何检测__init__.py当前的模块对象? I tried imp.find_module , but it seems it doesn't return a module object. 我尝试了imp.find_module ,但似乎没有返回模块对象。

OK , I update the problem here. 好的,我在这里更新问题。 First , module1 is really what I want to use in code, but It depends on module2 and module3, and both module1 , module2 and module3 are from opensource code. 首先,module1确实是我想在代码中使用的东西,但是它取决于module2和module3,而module1,module2和module3都来自开源代码。 In order to keep it in sync with the newest version, so module1,2,3 remains what it look like after unzipped. 为了使其与最新版本保持同步,因此模块1,2,3保持解压缩后的样子。 We need to wrapper module1 to provdide some interface to other guys, they expected module1 is visible when calling "import module1" However, as it depends on module2,3 , so it is not callable. 我们需要包装module1给其他人提供一些接口,他们期望在调用“ import module1”时module1是可见的,但是,因为它依赖于module2,3,所以它是不可调用的。 So I want to wrapper it in a new "package", or just rename the "package" to "module1", so when others call "import package", it will transparently make the module1 namespace visible to caller. 因此,我想将其包装在新的“包”中,或者只是将“包”重命名为“ module1”,因此当其他人调用“ import package”时,它将透明地使module1名称空间对调用者可见。 I now could use imp.load_module to load module1, but when I called dir(package), it looks like this: 我现在可以使用imp.load_module加载module1,但是当我调用dir(package)时,它看起来像这样:

 >>> import package
>>> print dir(package)
    ['__all__', '__builtins__', '__doc__', '__file__', '__name__', '__package__', 'module1']
>>> print type(package.modue1)
 type<'module'>

So module1 is still not directly visible to the caller. 因此,module1仍对调用者不直接可见。 I noticed that I could update the attribute and member function of a module if I use tricky way like this: 我注意到,如果我使用如下棘手的方法,则可以更新模块的属性和成员函数:

>>> import package (this will triger calling __init__.py)
>>> package.__dict__.update(package.module1.__dict__)

after this, everything looks like if I have just called "import module1". 之后,一切看起来就像我刚刚调用了“ import module1”一样。 However, I could not get a module object after "import" return, I wonder if there is any method to upload the module dict in init .py ? 但是,在“ import”返回后我无法获得模块对象,我想知道是否有任何方法可以在init .py中上传模块字典?

Why do you want to do this? 为什么要这样做? Explicit is better than implicit! 显式胜于隐式!

Import everything your API wants to expose in __init__ then let people use from package import * instead. 导入您的API希望在__init__公开的所有内容,然后让人们from package import *使用。 Use __all__ to explicitly list what should be exported that way. 使用__all__明确列出应以这种方式导出的内容。

In __init__.py , use: __init__.py ,使用:

import module1
import module2

__all__ = ['module1', 'module2']

and leave importing everything to your users. 并将所有内容导入您的用户。 They should have the choice , not have the modules forced upon them. 他们应该选择 ,而不要强加任何模块。

Or, if you meant items from the nested modules to be visible in package , import those explicitly, or use from modulename import * there too (but do add an __all__ list to such modules): 或者,如果你的意思是嵌套模块项目将在可见的package ,导入这些明确的,或者使用from modulename import *有过(但不要添加一个__all__列表,这些模块):

from module1 import *
from module1 import __all__ as module1_exported
from module2 import *
from module2 import __all__ as module2_exported

__all__ = module1_exported + module2_exported

Adding items to __builtins__ means you made the names visible everywhere , not just to whatever imported your package . __builtins__添加项意味着您可以在任何地方看到名称,而不仅是导入package的名字。 Rarely is there a need to mess with built-ins. 很少有需要弄乱内置函数的。

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM