python中模块没有属性_Python错误:AttributeError:'模块'对象没有属性

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)

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

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

def mod12():

print "mod12"

Now, I run python, then import lib, which works OK, then lib.mod11() or 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.

It seems I'm missing something really simple.

(I'm using Python 2.6 in Ubuntu 10.10)

Thank you

解决方案

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.

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

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.

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值