[英]Python error: AttributeError: 'module' object has no attribute
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).我对 Python 完全陌生,我知道这个问题被问了很多次,但不幸的是,我的情况似乎有点不同......我已经创建了一个包(或者我认为是这样)。 The catalog tree is like this:
目录树是这样的:
mydir
lib (__init__.py)
mod1 (__init__.py, mod11.py)
In parenthesis there are files in the catalog.括号中是目录中的文件。 Both
__init__.py
files are zero length. __init__.py
文件的长度都为零。
The file mydir/lib/mod1/mod11.py
contains 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
,然后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 mod11
or mod12
either.实际上,步骤 2 之后的
dir(lib)
也不显示mod11
或mod12
。 It seems I'm missing something really simple.看来我错过了一些非常简单的东西。
(I'm using Python 2.6 in Ubuntu 10.10) (我在 Ubuntu 10.10 中使用 Python 2.6)
Thank you谢谢
When you import lib
, you're importing the package.当您
import lib
,您正在导入包。 The only file to get evaluated and run in this case is the 0 byte __init__.py
in the lib directory.在这种情况下,唯一要评估和运行的文件是 lib 目录中的 0 字节
__init__.py
。
If you want access to your function, you can do something like this from lib.mod1 import mod1
and then run the mod12
function like so mod1.mod12()
.如果你想访问你的函数,你可以
from lib.mod1 import mod1
做这样的事情,然后像mod1.mod12()
一样运行mod12
函数。
If you want to be able to access mod1
when you import lib
, you need to put an import mod1
inside the __init__.py
file inside the lib
directory.如果您希望在导入
lib
时能够访问mod1
,则需要在lib
目录内的__init__.py
文件中放置一个import mod1
。
More accurately, your mod1
and lib
directories are not modules, they are packages.更准确地说,您的
mod1
和lib
目录不是模块,而是包。 The file mod11.py
is a module.文件
mod11.py
是一个模块。
Python does not automatically import subpackages or modules. Python 不会自动导入子包或模块。 You have to explicitly do it, or "cheat" by adding import statements in the initializers.
您必须明确地这样做,或者通过在初始值设定项中添加导入语句来“欺骗”。
>>> 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 from
syntax 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()
。
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。
My solution is put those imports in __init__.py
of lib:我的解决方案是将这些导入放在 lib 的
__init__.py
中:
in file: __init__.py
import mod1
Then,然后,
import lib
lib.mod1
would work fine.会工作得很好。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.