[Tutor] import problem
Chad Crabtree
flaxeater at yahoo.com
Sun May 9 09:55:23 EDT 2004
--- pan at uchicago.edu wrote:
> Hi,
>
> 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 ...
>
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
http://hotjobs.sweepstakes.yahoo.com/careermakeover
More information about the Tutor
mailing list