[Tutor] How to use a module when only import package
leechau
charlze at sohu.com
Fri Apr 8 05:58:16 CEST 2011
I wrote module1 in package1, and want to use a method named 'method1' in
module1, the caller(test.py) is like this:
import package1
package1.module1.method1()
module1.py is like this:
def method1():
print 'method1 run!'
The filesystem structure is:
test.py
package1
|__ __init__.py
|__ module1.py
When i run test.py, the output is:
AttributeError: 'module' object has no attribute 'module1'
File "e:\MyDoc\GODU_BVT\test.py", line 2, in <module>
package1.module1.method1()
If test.py is modified to:
import package1.module1
...
then everything goes well. Python documentation said:
"Contrarily, when using syntax like import item.subitem.subsubitem, each
item except for the last must be a package; the last item can be a
module or a package but can’t be a class or function or variable defined
in the previous item."
Obviously we can import a package besides a module, so i don't know why
the "import package1" style does not work. Above codes tested on python
2.6.6, windows xp sp2. Thanks for explanations :)
More information about the Tutor
mailing list