一个包中的python导入模块 [英] python import module from a package

查看:163
本文介绍了一个包中的python导入模块的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

文件夹结构:

<current dir>
   main.py
   packages <dir>
      __init__.py
      mod.py

main py:

import packages
print packages.mod.hello()

mod.py:

def hello():
    return 'hello'

__init __。py:

from packages import mod

如果我运行 main.py ,我没有收到任何错误。但是如果我从包import *'编辑 __ init __。py ',我会收到此错误: AttributeError :'module'对象没有属性'mod'

If I run main.py, I get no error. But if I edit __init__.py to 'from packages import *' , I get this error: AttributeError: 'module' object has no attribute 'mod'

我不是在问这个'print'命令工作。我可以在 main.py 中使用其他'import'语法来使其正常工作。问题是:我对 __ init __。py 中的包导入mod'中的'感到好奇。如果我可以导入mod 然后当我替换为 import * ,这意味着导入所有东西,为什么我得到一个而不是错误?

I'm not asking how to make that 'print' command work. I can use other 'import' syntax in main.py to make it work. The question is: I'm curious about that 'from packages import mod' in the __init__.py. If i can do import mod then when I replace to import *, which means import everything, why do I get an error instead?

那么来自包导入* __ init__中真正意味着什么? .py

任何人都可以提供帮助吗?谢谢

Anyone can help? Thanks

推荐答案

简短回答



那么包裹导入的 * 真正意味着在 __ init __。py

Short answer

So what does the from packages import * really mean inside that __init__.py?

__ init __。py 自行导入。

您只能导入模块,而不能导入包。包只是模块或子包的容器。当你导入一个包时,你实际上导入了模块 __ init __。py

You can only import modules, not packages. Packages are just containers for modules or sub-packages. When you "import" a package you actually import the module __init__.py.

__ init __。py 使用此内容:

from packages import mod

将模块 mod 导入 __ init __。py 。因此,您的 main.py 将通过 packages.mod 提供
(请记住 packages __ init __。py )表示。

imports the module mod into __init__.py. Therefore, it will be available in your main.py via packages.mod (remember packages is represented by __init__.py).

当你改变时来自包裹进口的$ code> __ init __。py 的内容:

When you change the content of __init__.py to:

from packages import *

您正在导入模块 __ init __。py ,你在同一个文件。
这个工作(第二个导入只是触发 sys.modules 中的查找)
但不会给你 mod 的内容。

You are importing the module __init__.py, the very same file you are in. This works (a second import just triggers a lookup in sys.modules) but won't give you the content of mod.

这意味着,您可以使用:

This means, you can use:

from module import *

但你不能合理地使用空白<​​code> __ init __。py :

from package import *

因为实际上由 __ init __。py 表示,并且
还没有。你可以检查这个(交互式或文件中):

Because package is actually represented by the __init__.py and there is nothing in it yet. You can check this (interactively or in file):

>>> import packages
>>> print(packages)
<module 'packages' from '/.../packages/__init__.py'>

__ init __。py 中你可以写:

from packages.mod import *

然后在 main.py

print packages.hello()

有效。因为函数 hello()现在位于
文件的全局名称空间 __ init __。py

works. Because the function hello() is now in the global name space of the file __init__.py.

正如mozman的回答中所提到的,你可以在 __ init__.py中使用 __ all __
列出中的,则应导入nofollow>。这是为这种情况设计的。

As mentioned in the answer by mozman, you can use __all__ in __init__.py to list the modules that should be imported if from packages import * is used. This is designed for this case.

__ init __。py 只有这个内容:

__all__ = ['mod']

现在你可以在 main.py 中执行此操作:

Now you can do this in main.py:

from packages import *

print mod.hello()

如果你扩展你的 __ init __。py

__all__ = ['mod']

from packages import *

您可以在<$ c $中执行此操作c> main.py

import packages

print packages.mod.hello()

但如果你从包中删除导入* 来自 __ init __。py

But if you remove the from packages import * from __init__.py:

__all__ = ['mod'] 

您将收到错误消息:

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

因为 _ _all __ 仅用于包import * 案例中的
现在我们回到 __ init __。py 导入本身。

because the __all__ is only used for the from packages import * case. Now we are back to the __init__.py imports itself.

这篇关于一个包中的python导入模块的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆