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

G

GKalman

OS= MS Vista
File structure:
..../Module (i.e a Folder with 2 sub-folders)
.../Module_Class (sub-folder #1)
/MyClass.py
.../Module_Class_Testing (sub_folder #2)
/module_class_driver.py

Here is the code for the two (simplified) Python files:


#this is module_class_driver.py
#======================
import sys

dir1='C:\\Programming\\Python\\Prototypes\\Core_Python_Prototypes\\Module\\Module_Class'
sys.path.append(dir1)


from MyClass import *
from MyOtherClass import * # error msg: no such module!

c=MyClass(2.5)
print c.double_it()

cc=MyOtherClass(5.0) #error
print cc.triple_it() #error


#this is MyClass.py
#============
class MyClass:
def __init__(self,x):
self.x=x
def double_it(self):
return 2*self.x

class MyOtherClass:
def __init__(self,y):
self.y=y
def triple_it(self):
return 3*self.y

Question:
*******
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?
 
A

alex23

GKalman said:
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?
 
G

GKalman

alex23 said:
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?

Alex!
Looks like I did misunderstand how 'import' works. Thanks for your help, now
it works OK.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,011
Latest member
AjaUqq1950

Latest Threads

Top