简体   繁体   English

如何从包中导入包?

[英]How can I import a package from within the package?

I'm writing a little package and I'm trying to include a demo script within it as an example. 我正在编写一个小程序包,并尝试在其中包含一个演示脚本作为示例。 However, I can't seem to import the package cleanly from within as though I was outside of it. 但是,我似乎无法像从外部那样从内部干净地导入该软件包。

With a directory structure like: 具有如下目录结构:

trainer/
  __init__.py
  helper.py
  trainer.py
  [...more files...]
  demo.py

In demo.py I can't do from .. import trainer as it complains "Attempted relative import in non-package", despite the __init__.py . demo.py ,尽管使用了__init__.py ,但我无法from .. import trainer执行此操作from .. import trainer因为它抱怨“未打包的相对导入尝试”。 If I move the demo up a directory and import trainer it works fine, but I was trying to keep it together with the package. 如果我将演示文件移至目录并import trainer它可以正常工作,但是我试图将其与软件包保持在一起。

The hack-looking import __init__ as trainer works, but eeeew. 看起来很hack的import __init__ as trainer可以用,但是很安全。

Importing the various bits from all over the module directly also works, but makes for a messy example. 从整个模块直接导入各个位也是可行的,但这使例子变得混乱。 Am I wholly misguided in my attempt or is there a better solution? 我是完全被误导了,还是有更好的解决方案?

If you're trying to run demo.py as python demo.py , the problem that you're having is likely the same as here . 如果您尝试将demo.py作为python demo.py运行,那么您遇到的问题可能与此处相同。

What's happening is that Python's relative import mechanism works by using the __name__ of the current module. 发生的事情是Python的相对导入机制通过使用当前模块的__name__ When you execute a module directly, the __name__ gets set "__main__" regardless what the actual module name is. 直接执行模块时,无论实际的模块名称是什么, __name__都会设置为"__main__" Thus, relative (in-package) imports don't work. 因此,相对(包装内)进口不起作用。

To remedy this, you can do the following: 为了解决这个问题,您可以执行以下操作:

  • Execute demo.py as a module within a package , like so: python -m trainer.demo . demo.py 作为包中的模块执行,如下所示: python -m trainer.demo This should fix the error, but you'll still be importing the trainer.py module instead of the package. 这应该可以解决该错误,但是您仍将导入trainer.py模块而不是软件包。

  • Now add from __future__ import absolute_import to demo.py , which will cause your imports to be absolute-only by default, meaning that relative imports have to explicit (as in, from . import (...) ). 现在,将from __future__ import absolute_import添加到demo.py ,这将默认使您的导入为绝对绝对导入,这意味着相对导入必须是显式的(例如demo.py from . import (...) )。 This is force import trainer to import the entire top-level package, instead of the module. 这是强制import trainer导入整个顶级程序包而不是模块的步骤。

The way you organize the files, demo.py becomes part of the package, which might or might not be what you want. 整理文件的方式demo.py成为包的一部分,可能不是您想要的。 You can organize your files a little differently, moving demo.py outside of the trainer directory: 您可以组织文件的方式有点不同,移动培训师目录demo.py外:

TopDir/
    demo.py
    trainer/
    __init__.py
    helper.py
    trainer.py
    [... more files ...]

Then, demo.py can do something like: 然后,demo.py可以执行以下操作:

from trainer import trainer, helper

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

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