1

FIXED: turns out there is a module already called parser. Renamed it and its working fine! Thanks all.

I got a python NameError I can't figure out, got it after AttributeError. I've tried what I know, can't come up with anything.

main.py:

from random import *
from xml.dom import minidom
import parser
from parser import *
print("+---+ Roleplay Stat Reader +---+")
print("Load previous DAT file, or create new one (new/load file)")
IN=input()
splt = IN.split(' ')
if splt[0]=="new":
    xmlwrite(splt[1])
else:
    if len(splt[1])<2:
        print("err")
    else:
        xmlread(splt[1])
ex=input("Press ENTER to Exit...")

parser.py:

from xml.dom import minidom
from random import *
def xmlread(doc):
    xmldoc = minidom.parse(doc)
    itemlist = xmldoc.getElementsByTagName('item')
    for s in itemlist:
            print(s.attributes['name'].value,":",s.attributes['value'].value)

def xmlwrite(doc):
    print("no")

And no matter what I get the error:

Traceback (most recent call last):
  File "K:\Python Programs\Stat Reader\main.py", line 10, in <module>
    xmlwrite.xmlwrite(splt[1])
NameError: name 'xmlread' is not defined

The same error occurs when trying to access xmlwrite.

When I change xmlread and xmlwrite to parser.xmlread and parser.xmlwrite I get:

Traceback (most recent call last):
  File "K:\Python Programs\Stat Reader\main.py", line 15, in <module>
    parser.xmlread(splt[1])
AttributeError: 'module' object has no attribute 'xmlread'

The drive is K:\ because it's my personal drive at my school.

Tassaris
  • 11
  • 2
  • I would go into the interpreter, import parser, dir(parser), and see if you are importing the right module or if it is importing the std-lib parser module. – Radio- May 15 '13 at 14:33

1 Answers1

0

If your file is really called parser.xml, that's your problem. It needs to be parser.py in order to work.

EDIT: Okay, since that wasn't your issue, it looks like you have a namespacing issue. You import your parser module twice when you use import parser and then from parser import *. The first form of it makes "parser" the namespace and the second form directly imports it, so in theory, you should have both parser.xmlwrite and xmlwrite in scope. It's also clearly not useful to import minidom in main.py since you don't use any minidom functionality in there.

If you clear up those and still have the issue, I would suggest looking at __ init __.py. If that still does nothing, it could just plain be a conflict with Python's parser module, you could substitute a name like myxmlparser.

Community
  • 1
  • 1
Joel
  • 249
  • 2
  • 10