Python 错误:AttributeError:“模块"对象没有属性 [英] Python error: AttributeError: 'module' object has no attribute

查看:47
本文介绍了Python 错误:AttributeError:“模块"对象没有属性的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我对 Python 完全陌生,我知道这个问题被问过很多次,但不幸的是,我的情况似乎有点不同......我已经创建了一个包(或者我认为).目录树是这样的:

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:

mydir
    lib   (__init__.py)
        mod1  (__init__.py, mod11.py)

括号中是目录中的文件.两个 __init__.py 文件都是零长度.

In parenthesis there are files in the catalog. Both __init__.py files are zero length.

文件 mydir/lib/mod1/mod11.py 包含以下内容:

The file mydir/lib/mod1/mod11.py contains the following:

def mod12():
    print "mod12"

现在,我运行 python,然后运行 ​​import lib,运行正常,然后运行 ​​lib.mod11()lib.mod12().

Now, I run python, then import lib, which works OK, then lib.mod11() or lib.mod12().

最后两个中的任何一个都给了我主题错误消息.实际上,步骤 2 之后的 dir(lib) 也不显示 mod11mod12 .看来我错过了一些非常简单的东西.

Either of the last two gives me the subject error message. Actually dir(lib) after Step 2 does not display mod11 or mod12 either. It seems I'm missing something really simple.

(我在 Ubuntu 10.10 中使用 Python 2.6)

(I'm using Python 2.6 in Ubuntu 10.10)

谢谢

推荐答案

当你 import lib 时,你正在导入包.在这种情况下,唯一要评估和运行的文件是 lib 目录中的 0 字节 __init__.py.

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__.py in the lib directory.

如果你想访问你的函数,你可以执行类似 from lib.mod1 import mod1 的操作,然后像 mod1 那样运行 mod12 函数.mod12().

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().

如果你想在导入lib的时候能够访问mod1,你需要在里面放一个import mod1lib 目录中的 __init__.py 文件.

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.

这篇关于Python 错误:AttributeError:“模块"对象没有属性的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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