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

Discussion in 'Python' started by GKalman, May 11, 2009.

  1. GKalman

    GKalman Guest

    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-fro...re-than-one-Class...-tp23476815p23476815.html
    Sent from the Python - python-list mailing list archive at Nabble.com.
    GKalman, May 11, 2009
    #1
    1. Advertising

  2. GKalman

    alex23 Guest

    GKalman <> 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?
    alex23, May 11, 2009
    #2
    1. Advertising

  3. GKalman

    GKalman Guest

    alex23 wrote:
    >
    > GKalman <> 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-fro...re-than-one-Class...-tp23476815p23481160.html
    Sent from the Python - python-list mailing list archive at Nabble.com.
    GKalman, May 11, 2009
    #3
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. plb
    Replies:
    2
    Views:
    341
  2. Merciadri Luca
    Replies:
    4
    Views:
    802
  3. Ala Qumsieh
    Replies:
    5
    Views:
    64
    Michele Dondi
    Dec 19, 2003
  4. Steven D'Aprano
    Replies:
    0
    Views:
    81
    Steven D'Aprano
    Dec 23, 2013
  5. Replies:
    3
    Views:
    74
    Gary Herron
    Dec 23, 2013
Loading...

Share This Page