[Tutor] import problem
flaxeater at yahoo.com
Sun May 9 09:55:23 EDT 2004
--- pan at uchicago.edu wrote:
> I wanna import a class MyClass from a module
> myModule.py but something
> weird just happened :
>  import myModule ===> ok
>  from myModule import * ===> ok
>  from myModule import MyClass ===> ImportError:
> cannot import name MyClass
>  But when I put  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
> "from myModule import MyClass" is placed globally,
> no problem at all when it is inside a class ????
> This is really confusing ...
Well I do not believe there is quite enough
information. If the code you showed is all that is in
the script then it would apear to work but not really
work because it has not been instantiated.
Investigate a little. You should at the interactive
prompt type import myModule
then type dir(myModule) and see if your class is
there. If not then something strange has happend I'm
sure. In addition when you do this paste a traceback
of the error.
Do you Yahoo!?
Win a $20,000 Career Makeover at Yahoo! HotJobs
More information about the Tutor