简体   繁体   中英

global name 'class' is not defined - Python

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 ...

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 *

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM