为什么 Python 模块有时不导入其子模块?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/3781522/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-18 12:42:23  来源:igfitidea点击:

Why do Python modules sometimes not import their sub-modules?

pythonpython-import

提问by chriscauley

I noticed something weird today I would like explained. I wasn't 100% sure how to even phrase this as a question, so google is out of the question. The logging module does not have access to the module logging.handlers for some odd reason. Try it yourself if you don't believe me:

我今天注意到一些奇怪的事情,我想解释一下。我不是 100% 确定如何将其表述为一个问题,所以谷歌是不可能的。由于某些奇怪的原因,日志模块无权访问模块 logging.handlers。如果你不相信我,你自己试试看:

>>> import logging
>>> logging.handlers
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
AttributeError: 'module' object has no attribute 'handlers'
>>> import logging.handlers
>>> logging.handlers
<module 'logging.handlers' from '/usr/lib/python2.6/logging/handlers.pyc'>

Can anyone explain why this happens?

谁能解释为什么会发生这种情况?

采纳答案by Thomas Wouters

In Python, modules need to be imported before they're accessible. import loggingimports just the logging module. It so happens that loggingis a package with submodules, but those submodules are still not automatically loaded. So, you need to explicitly import logging.handlersbefore you can access it.

在 Python 中,需要先导入模块,然后才能访问它们。import logging只导入日志模块。碰巧这logging是一个带有子模块的包,但这些子模块仍然没有自动加载。因此,您需要先显式导入,logging.handlers然后才能访问它。

If you're wondering why it looks like sometimes you don't need those extra imports: some packages import some or all of their submodules when they are imported -- simply by doing those imports in their __init__.pyfiles. In other cases it might be that something else that you import, also imported logging.handlers. It doesn't matter which piece of code does the import; as long as somethingin your process imports logging.handlersbefore you access it, it'll be there. And sometimes a module that looks like a package really isn't one, like osand os.path. osisn't a package, it just imports the correct other module (for your platform) and calls it path, just so you can access it as os.path.

如果您想知道为什么有时看起来不需要那些额外的导入:某些包在导入时会导入部分或全部子模块——只需在它们的__init__.py文件中进行这些导入。在其他情况下,可能是您导入的其他内容也导入了logging.handlers. 导入哪一段代码并不重要;只要您的流程中的某些内容在您logging.handlers访问它之前导入,它就会在那里。有时看起来像包的模块实际上不是一个,比如osos.pathos不是一个包,它只是导入正确的其他模块(适用于您的平台)并调用它path,这样您就可以以os.path.

回答by shahjapan

I'm also new to python and after having lots of practice now I can differentiate between, package (folder) , module(.py) , classes,variables...etc...

我也是 python 的新手,经过大量练习后,现在我可以区分包(文件夹)、模块(.py)、类、变量...等...

if you want any of your folder to be python package - It must contain __init__.pyfile even empty file will do !!!

如果你想让你的任何文件夹成为 python 包 - 它必须包含__init__.py文件,即使是空文件也可以!!!

and as Thomas said, you can import extra module in __init__.py if you want !!! but modules/packages are accessible only after importing it...

正如托马斯所说,__init__.p如果你愿意,你可以在y 中导入额外的模块!!!但是模块/包只有在导入后才能访问...

if you want to import everything from a module you can use

如果你想从一个模块中导入所有的东西,你可以使用

from logging import *

rest you can access the handlers module like below too,

休息你也可以像下面一样访问处理程序模块,

from logging import handlers
print dir(handlers)

回答by Alexey

I've faced recently the same odd situation. So, I bet you've removed some third-party lib import. That removed lib contained from logging import handlersor from logging import *and provided you handlers. And in other script you've had something like import loggingand just used logging.handlersand you've thought that is a way things work as I did.

我最近遇到了同样奇怪的情况。所以,我敢打赌你已经删除了一些第三方库导入。删除的 lib 包含 from logging import handlersfrom logging import *并为您提供handlers. 在其他脚本中,您已经使用了类似的东西import logginglogging.handlers并且您认为这是一种像我一样工作的方式。

回答by Underflow

Thomas Woutersanswered this question very well, but alas, I only found this question after finding the answer in the original documentation. To that end I thought I would add to this in the hopes it pops up closer to the top of the search engine in the future.

Thomas Wouters很好地回答了这个问题,但唉,我是在原始文档中找到答案后才发现这个问题的。为此,我想我会添加它,希望它在未来更接近搜索引擎的顶部。

QUESTION

Why does the error: 'AttributeError: module 'module_name' has no attribute 'sub_module_name' appear even though my editor (e.g. Visual Code) auto-completes the sub-module name:

为什么会出现错误:' AttributeError: module ' module_name' has no attribute ' sub_module_name' 即使我的编辑器(例如 Visual Code)自动完成子模块名称:

 import module_name
 module_name.sub_module_name(parameter)

ANSWER

回答

Your editor is basing its autocomplete off of the file structure of your project and not off of Python behavior. Sub-modules are not 'automatically' imported when you import a module. Reference Python Documentationfor details on how to 'automatically' import sub-modules when using

您的编辑器的自动完成功能基于项目的文件结构,而不是基于 Python 行为。导入模块时,子模块不会“自动”导入。参考Python文档使用时的详细信息,如何“自动”导入子模块

 import module_name

The key contribution with this answer being the addition of AttributeError when trying to import a 'module' or 'package'

这个答案的关键贡献是在尝试导入“模块”或“包”时添加了 AttributeError

Hope this helps someone!

希望这可以帮助某人!