469,285 Members | 2,588 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,285 developers. It's quick & easy.

import help

Hi all,

I have the following class code for a Node for a Linked List, (saved in
node.py)
class Node :
def __init__(self,element):
self.element = element
self.next = None
then i have another module called LinkedList.py, with following code

import sys
import node

def main():
print "Does nothin ATM, as it doesnt work"

main()
the problem im having is that im getting NameError exception when i run
LinkedList.py "global name 'Node' is not defined"

Does anyone know what the problem is here?

Thanks in advance

Apr 11 '06 #1
4 1993
placid wrote:
I have the following class code for a Node for a Linked List, (saved in
node.py)

class Node :
def __init__(self,element):
self.element = element
self.next = None

then i have another module called LinkedList.py, with following code

import sys
import node
def main():
print "Does nothin ATM, as it doesnt work"
main()

the problem im having is that im getting NameError exception when i run
LinkedList.py "global name 'Node' is not defined"

Does anyone know what the problem is here?


I have trouble believing you. I suspect you are telling us what you
think is happening, and leaving something vital out. In particular,
cut and paste the traceback.

Here's another experiment, interactively do:

import node
print node.__file__

Perhaps that will tell you something.

--Scott David Daniels
sc***********@acm.org
Apr 11 '06 #2

Scott David Daniels wrote:
placid wrote:
I have the following class code for a Node for a Linked List, (saved in
node.py)

class Node :
def __init__(self,element):
self.element = element
self.next = None

then i have another module called LinkedList.py, with following code

import sys
import node
def main():
print "Does nothin ATM, as it doesnt work"
main()

the problem im having is that im getting NameError exception when i run
LinkedList.py "global name 'Node' is not defined"

Does anyone know what the problem is here?
I have trouble believing you. I suspect you are telling us what you


im not leaving anything out, im telling you what's happening
think is happening, and leaving something vital out. In particular,
cut and paste the traceback.
here it is!

Traceback (most recent call last):
File "LinkedList.py", line 7, in ?
main()
File "LinkedList.py", line 4, in main
n = Node("test")
NameError: global name 'Node' is not defined


Here's another experiment, interactively do:

import node
print node.__file__
import node
print node.__file__

node.py

Perhaps that will tell you something.
More information, im using Windows XP Pro, these two files are in same
directory...

--Scott David Daniels
sc***********@acm.org


Apr 11 '06 #3
so there is a record of this problem i had, to solve this problem
either use

import node
Apr 11 '06 #4

Dennis Lee Bieber wrote:
On 10 Apr 2006 20:55:31 -0700, "placid" <Bu****@gmail.com> declaimed the
following in comp.lang.python:


Traceback (most recent call last):
File "LinkedList.py", line 7, in ?
main()
File "LinkedList.py", line 4, in main
n = Node("test")
NameError: global name 'Node' is not defined

That is NOT the same code as you posted.

You posted:
import sys
import node

def main():
print "Does nothin ATM, as it doesnt work"

main()

Line 4 of that is "def main():"

If you have any .pyc or .pyo files in the same directory as your
source, delete them, try again, and show us what happened.

BTW: as for the code that is being run... IT NEEDS

n = node.Node("test")

You imported a module "node", but the class definition named "Node"
is INSIDE the module -- so you have to specify
module.class
to access it...
--
> ================================================== ============ <
> wl*****@ix.netcom.com | Wulfraed Dennis Lee Bieber KD6MOG <
> wu******@dm.net | Bestiaria Support Staff <
> ================================================== ============ <
> Home Page: <http://www.dm.net/~wulfraed/> <
> Overflow Page: <http://wlfraed.home.netcom.com/> <

Sorry, i left some code out, my bad, LinkedList.py contains (well it
doesnt anymore, but originally did)

import node

def main():
n = Node("Test")
print n.element

main()
but now i know why this doesnt work and the solution (see previous post
of mine)
thanks for all the help!

Apr 11 '06 #5

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

5 posts views Thread by Steve Holden | last post: by
3 posts views Thread by Olivier Noblanc ATOUSOFT | last post: by
2 posts views Thread by David Berry | last post: by
1 post views Thread by Robin Becker | last post: by
4 posts views Thread by khutch | last post: by
7 posts views Thread by Ron Adam | last post: by
5 posts views Thread by Stef Mientki | last post: by
9 posts views Thread by rsoh.woodhouse | last post: by
1 post views Thread by CARIGAR | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.