[Tutor] import problem

pan at uchicago.edu pan at uchicago.edu
Sun May 9 01:02:06 EDT 2004


I wanna import a class MyClass from a module myModule.py but something
weird just happened :

[1] import myModule   ===> ok

[2] from myModule import * ===> ok

[3] from myModule import MyClass ===> ImportError: cannot import name MyClass

[4] But when I put [3] inside a class:

    class NewClass(object):
      def __init__(self):
         from myModule import MyClass    # <======= 
         self.item = MyClass()
    x = NewClass()
    print x.item.__doc__

    ===> THIS IS OK !!!

How come a class can't be imported when the statement:
"from myModule import MyClass" is placed globally, but 
no problem at all when it is inside a class ????

This is really confusing ...


More information about the Tutor mailing list