Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Python > Importing from a module which contains more than one Class...

Reply
Thread Tools

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

 
 
GKalman
Guest
Posts: n/a
 
      05-11-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_Py thon_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...p23476815.html
Sent from the Python - python-list mailing list archive at Nabble.com.

 
Reply With Quote
 
 
 
 
alex23
Guest
Posts: n/a
 
      05-11-2009
GKalman <(E-Mail Removed)> 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?
 
Reply With Quote
 
 
 
 
GKalman
Guest
Posts: n/a
 
      05-11-2009



alex23 wrote:
>
> GKalman <(E-Mail Removed)> 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...p23481160.html
Sent from the Python - python-list mailing list archive at Nabble.com.

 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Can one declare more than one signal on one line? Merciadri Luca VHDL 4 11-01-2010 02:00 PM
How to keep a module with the same name as a module it is importing from importing itself? plb Python 2 02-08-2005 03:14 PM
The number name 'System.Web.UI.WebControls' contains more than the maximum number of prefixes. The maximum is 3. mayur ASP .Net Web Controls 2 07-16-2004 05:14 PM
The number name 'System.Web.UI.WebControls' contains more than the maximum number of prefixes. The maximum is 3. mayur ASP .Net 2 07-02-2004 10:35 AM
Regex problem, match if line contains <a>, unless it also contains <b> James Dyer Perl 5 02-20-2004 12:29 PM



Advertisments