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

alex23 wuwei23 at gmail.com
Mon May 11 06:19:52 CEST 2009


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?



More information about the Python-list mailing list