0

I defined a class called Parser in a file called parser.py, that parses a test result....

import sys
import re

class Parser:

    def __init__(self):
        pass

    def udp_parse(self, filename=""):
       # ... some code over here

Now, in main.py. I have:

from dbconn import *
from parser import *
import os
import subprocess

def main() 
    dbconn = Dbconn()
    parse = Parser()
    # more code here ...

if __name__ == '__main__':
    main()

and I'm getting:

Traceback (most recent call last):
  File "iperf.py", line 108, in <module>
    main()
  File "iperf.py", line 49, in main
    parse = Parser()
NameError: global name 'Parser' is not defined

parser.py is in the same directory as dbconn.py and main.py. Dbconn() works without any problem, but I'm not understanding why Parser() can't work too ...

1 Answer 1

4

Parser is an existing python module. Use a different file name (not parser.py) or insert your path in sys.path before stdlibs.

import sys
sys.path.insert(0,'parser/directory')
from parser import *
2
  • changing the file name would do ? Aug 12, 2013 at 23:21
  • @philippe yes, it should. as long as it doesn't share a name with another python module.
    – sihrc
    Aug 12, 2013 at 23:21

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.