Python NameError when trying to use custom class

I get a weird instance of NameError when I try to use the class I wrote. In the directory, I have the following file structure:

  • dir /
    • ReutersParser.py
    • test.py
    • Reut-xxx.sgm

Where is my custom class defined in ReutersParser.py and I have a test script defined in test.py.

ReutersParser looks something like this:

from sgmllib import SGMLParser

class ReutersParser(SGMLParser):

    def __init__(self, verbose=0):
        SGMLParser.__init__(self, verbose)

    ... rest of parser

if __name__ == '__main__':

    f = open('reut2-short.sgm')
    s = f.read()

    p = ReutersParser()
    p.parse(s)

This is a parser for working with SGML files of Reuters articles. The test works great. Anyway, I'm going to use it in test.py, which looks like this:

from ReutersParser import ReutersParser

def main():
    parser = ReutersParser()

if __name__ == '__main__':
    main()

When it reaches this line of the analyzer, I get this error:

Traceback (most recent call last):
  File "D:\Projects\Reuters\test.py", line 34, in <module>
    main()
  File "D:\Projects\Reuters\test.py", line 19, in main
    parser = ReutersParser()
  File "D:\Projects\Reuters\ReutersParser.py", line 38, in __init__
    SGMLParser.__init__(self, verbose)
NameError: global name 'sgmllib' is not defined

- , ReutersParser test.py, , , sgmllib, . , , .

NameError? sgmllib test.py, , , ReutersParser.

+3
1

, , . , , .

  File "D:\Projects\Reuters\ReutersParser.py", line 38, in __init__
    SGMLParser.__init__(self, verbose)
NameError: global name 'sgmllib' is not defined

, 'sgmllib' , Python , . : ( Python ), , , , . , , IDE, Python . , . , sgmllib.SGMLParser.__init__(self, verbose) - .

, ​​ , , - - , - , IDE , () , . (, ), , , IDE . , , .

+3

Source: https://habr.com/ru/post/1740606/


All Articles