Python错误:AttributeError:'module'对象没有属性
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/4871369/
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
Python error: AttributeError: 'module' object has no attribute
提问by Alex
I'm totally new to Python and I know this question was asked many times, but unfortunately it seems that my situation is a bit different... I have created a package (or so I think). The catalog tree is like this:
我对 Python 完全陌生,我知道这个问题被问过很多次,但不幸的是,我的情况似乎有点不同......我已经创建了一个包(或者我认为是这样)。目录树是这样的:
mydir
lib (__init__.py)
mod1 (__init__.py, mod11.py)
In parenthesis there are files in the catalog. Both __init__.pyfiles are zero length.
括号中是目录中的文件。两个__init__.py文件的长度都为零。
The file mydir/lib/mod1/mod11.pycontains the following:
该文件mydir/lib/mod1/mod11.py包含以下内容:
def mod12():
print "mod12"
Now, I run python, then import lib, which works OK, then lib.mod11()or lib.mod12().
现在,我运行python, then import lib,运行正常,然后lib.mod11()或lib.mod12()。
Either of the last two gives me the subject error message. Actually dir(lib)after Step 2 does not display mod11or mod12either.
It seems I'm missing something really simple.
最后两个中的任何一个都给了我主题错误消息。实际上dir(lib)在第 2 步之后没有显示mod11或mod12两者都没有。看来我错过了一些非常简单的东西。
(I'm using Python 2.6 in Ubuntu 10.10)
(我在 Ubuntu 10.10 中使用 Python 2.6)
Thank you
谢谢
回答by Gary van der Merwe
The way I would do it is to leave the __ init__.py files empty, and do:
我这样做的方法是将 __ init__.py 文件留空,然后执行以下操作:
import lib.mod1.mod11
lib.mod1.mod11.mod12()
or
或者
from lib.mod1.mod11 import mod12
mod12()
You may find that the mod1 dir is unnecessary, just have mod12.py in lib.
您可能会发现 mod1 目录是不必要的,只需在 lib 中有 mod12.py。
回答by Noufal Ibrahim
When you import lib, you're importing the package. The only file to get evaluated and run in this case is the 0 byte __init__.pyin the lib directory.
当您 时import lib,您正在导入包。在这种情况下,唯一需要评估和运行的文件是__init__.pylib 目录中的 0 字节。
If you want access to your function, you can do something like this from lib.mod1 import mod1and then run the mod12function like so mod1.mod12().
如果你想访问你的函数,你可以做这样的事情from lib.mod1 import mod1,然后mod12像这样运行函数mod1.mod12()。
If you want to be able to access mod1when you import lib, you need to put an import mod1inside the __init__.pyfile inside the libdirectory.
如果您希望能够mod1在导入时访问lib,则需要在目录中import mod1的__init__.py文件中放置一个lib。
回答by Keith
More accurately, your mod1and libdirectories are not modules, they are packages. The file mod11.pyis a module.
更准确地说,您的mod1和lib目录不是模块,它们是包。该文件mod11.py是一个模块。
Python does not automatically import subpackages or modules. You have to explicitly do it, or "cheat" by adding import statements in the initializers.
Python 不会自动导入子包或模块。您必须明确地这样做,或者通过在初始值设定项中添加导入语句来“欺骗”。
>>> import lib
>>> dir(lib)
['__builtins__', '__doc__', '__file__', '__name__', '__package__', '__path__']
>>> import lib.pkg1
>>> import lib.pkg1.mod11
>>> lib.pkg1.mod11.mod12()
mod12
An alternative is to use the fromsyntax to "pull" a module from a package into you scripts namespace.
另一种方法是使用from语法将模块从包中“拉”到脚本命名空间中。
>>> from lib.pkg1 import mod11
Then reference the function as simply mod11.mod12().
然后简单地引用该函数mod11.mod12()。
回答by Henry
My solution is put those imports in __init__.pyof lib:
我的解决方案是将这些导入放在__init__.pylib 中:
in file: __init__.py
import mod1
Then,
然后,
import lib
lib.mod1
would work fine.
会工作得很好。

