473,398 Members | 2,812 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,398 software developers and data experts.

ActiveState Python won't call module function.

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.
Jul 18 '05 #1
5 1602
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.

-- ================================================== ============ <
wl*****@ix.netcom.com | Wulfraed Dennis Lee Bieber KD6MOG <
wu******@dm.net | Bestiaria Support Staff <
================================================== ============ <
Bestiaria Home Page: http://www.beastie.dm.net/ <
Home Page: http://www.dm.net/~wulfraed/ <


Jul 18 '05 #2
On Sun, 30 Nov 2003 08:56:22 +1300, Glenn Reed wrote:
------- 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()

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

It generates the following error:
NameError: name 'module2' is not defined


'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.
--
mailto: logan@phreaker(NoSpam).net

Jul 18 '05 #3
In article <bq**********@lust.ihug.co.nz>,
"Glenn Reed" <do******@nowhere.com> wrote:
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

And it isn't. 'module1.module2' is defined in your main
module, though.

thisClass = module1.module2()

should work fine. (Hint: module1 doesn't have to import itself
to get the symbols it defines.)

Regards. Mel.
Jul 18 '05 #4
Yep, thanks, that worked

Dennis Lee Bieber <wl*****@ix.netcom.com> wrote in message
news:do************@beastie.ix.netcom.com...
thisClass = module1.module2()


Jul 18 '05 #5
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.
Dennis Lee Bieber <wl*****@ix.netcom.com> wrote in message
news:do************@beastie.ix.netcom.com...
thisClass = module1.module2()


Jul 18 '05 #6

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.