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

GKalman kalman_g at msn.com
Sun May 10 23:57:05 EDT 2009


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?



-- 
View this message in context: http://www.nabble.com/Importing-from-a-module-which-contains-more-than-one-Class...-tp23476815p23476815.html
Sent from the Python - python-list mailing list archive at Nabble.com.




More information about the Python-list mailing list