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

查看:644
本文介绍了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包含以下内容:

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,则需要在lib目录内的__init__.py文件中放入import mod1.

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天全站免登陆