Importing from a module which contains more than one Class...

GKalman kalman_g at msn.com
Mon May 11 13:04:00 CEST 2009




alex23 wrote:
> 
> GKalman <kalma... at msn.com> wrote:
>> from MyClass import *
>> from MyOtherClass import *     # error msg: no such module!
>>
>> As I mentioned above,  the code for MyClass & MyOtherClass is in the same
>> file . This program only works with a single Class in a file. That is
>> when
>> the File name is the SAME as the Class name.
>>
>> How to import from a File which contains more than one (unrelated)
>> Classes?
> 
> You seem to have misunderstood how 'import' works, I strongly
> recommend re-reading the tutorial section:
> http://docs.python.org/tutorial/modules.html
> 
> Basically, the import statement has two forms:
> 
>    1. from <module> import <contents>
>    2. import <module>
> 
> So your example of 'from <classname> import *' just doesn't make a lot
> of sense within Python.
> 
> If MyClass.py contains two classes, MyClass and MyOtherClass, you
> should be able to import them with:
> 
>    from MyClass import MyClass, MyOtherClass
> 
> However, you _should_ be already getting _everything_ MyClass.py
> contains as you're using the grab-all asterisk. Could you open a
> python shell in your Module_Class folder, type 'from MyClass import
> MyClass, MyOtherClass', and past whatever traceback you get here?
> --
> http://mail.python.org/mailman/listinfo/python-list
> 
> 

Alex! 
Looks like I did misunderstand how 'import' works. Thanks for your help, now
it works OK.
-- 
View this message in context: http://www.nabble.com/Importing-from-a-module-which-contains-more-than-one-Class...-tp23476815p23481160.html
Sent from the Python - python-list mailing list archive at Nabble.com.




More information about the Python-list mailing list