ActiveState Python won't call module function.

G

Glenn Reed

Hi,

This is probably a really easy question and I apologize if it is already
covered in some faq somewhere.

These are my two source files:
------- module1.py -------------
class module2:
def __init__(self):
self.x=23
self.y=14
self.z=33
self.x1=self.x=3

#thisClass=module2()
# I added the above line in just to test it works here and it does.
------- tmod.py ---------------
# Test Module
import module1

thisClass = module2()

----------------------------------------------

It generates the following error:
Traceback (most recent call last):
File
"C:\Python22\Lib\site-packages\Pythonwin\pywin\framework\scriptutils.py",
line 301, in RunScript
exec codeObject in __main__.__dict__
File "C:\My Documents\PythonSrc\Temp\tmod.py", line 4, in ?
thisClass = module1()
NameError: name 'module2' is not defined
I realize I have created a different class name 'module2' to the filename
'module1' but I was thinking at the time the error may be due to a namespace
clash between the name of the file and the name of the class or the filename
hiding the classname etc.

But this has got me stumped and I would appreciate any help on it.

Thanks in advance.
Glenn.
 
D

Dennis Lee Bieber

Glenn Reed fed this fish to the penguins on Saturday 29 November 2003
11:56 am:
import module1

thisClass = module2()

thisClass = module1.module2()
But this has got me stumped and I would appreciate any help on it.
You have to tell it that "module2" is inside module1 <G> And that
would be true even if it were named module1 internally.

--
 
L

Logan

------- module1.py -------------
class module2:
def __init__(self):
self.x=23
self.y=14
self.z=33
self.x1=self.x=3

------- tmod.py ---------------
# Test Module
import module1

thisClass = module2()

'import module1' adds the name 'module1' to your namespace;
so, the name 'module2' is still unknown in your namespace.

In the above example, you have to write:

thisClass = module1.module2()

Alternatively, you might prefer the following:

from module1 import module2

thisClass = module2()

If you want to import more than just one class, the following
syntax will do this:

from module1 import module2, module3, module4

By the way: your naming conventions (module1 for the file and
module2 for the class) are not really intuitive :)

HTH, L.
 
G

Glenn Reed

Yep, thanks that worked. To the other person who replied I realize that
module1, module2 names that I was using were counterintuitive. It was just
for the sake of this example. Thanks to everyone for their help.

Glenn.
 

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

Forum statistics

Threads
473,769
Messages
2,569,579
Members
45,053
Latest member
BrodieSola

Latest Threads

Top