On 04/08/2011 11:13 AM, leechau wrote:
Steven wrote:
leechau wrote:
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()
[...]
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.

Yes, that is correct, and that is a deliberate design.

What if your package had a 1000 sub-modules, each of which were big? You
wouldn't want loading the main package to automatically load all 1000
sub-modules, if you only needed 1.

You either import the sub-module by hand:

import package1.module1

and now you can use package1.module1.method1 (not really a method,
actually a function). If you want module1 to automatically be available
after importing the package, include one of these in the package1
__init__.py file:

import module1  # should work in Python 2


and now package1 will include the module1 in its namespace.


--
Steven

Thanks for Steven's exellent and patient explanations. How should I do if automatically import a module in Python 3? Thanks again.

--
leechau
_______________________________________________
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor
Hello,

Test it simply!
You will get answer in 3 seconds.

;o)
Regards
Karim
_______________________________________________
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor

Reply via email to