Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 711649
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 14, 20262026-05-14T04:45:33+00:00 2026-05-14T04:45:33+00:00

I’m getting a weird instance of a NameError when attempting to use a class

  • 0

I’m getting a weird instance of a NameError when attempting to use a class I wrote. In a directory, I have the following file structure:

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

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

The 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)

It’s a parser to deal with SGML files of Reuters articles. The test works perfectly. 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 gets to that parser line, I’m getting 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

For some reason, when I try to use my ReutersParser in test.py, it throws an error that says it cannot find sgmllib, which is a built-in module. I’m at my wits’ end trying to figure out why the import won’t work.

What’s causing this NameError? I’ve tried importing sgmllib in my test.py and that works, so I don’t understand why it can’t find it when trying to run the constructor for my ReutersParser.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-14T04:45:33+00:00Added an answer on May 14, 2026 at 4:45 am

    Your problem is not your code, but what you run it in. If you read the error and the code it displays closely:

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

    you’ll notice there’s no reference to ‘sgmllib’ on the line that Python thinks produced this error. That means one of two things: either the error didn’t originate there (and Python is quite confused), or the code that’s being displayed is not the code that is being executed. The latter is quite common when you, for example, run your code in an IDE that doesn’t restart the Python interpreter between code executions. It will execute your old code, but when displaying the traceback will show the new code. I’m guessing you did sgmllib.SGMLParser.__init__(self, verbose) on that line at some point in the past.

    The reason it was fixed by renaming the class is probably that you did something — like editing the code — that caused the IDE to either restart the interpreter, properly clean it up or (by accident) reloaded the right module the right way for it to see the new code. Since you name your module after your class (which is bad style, by the way) I assume you renamed your module when you renamed your class, and so your IDE picked up the new code this time. Until the next time the same thing happens, of course.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Ask A Question

Stats

  • Questions 368k
  • Answers 368k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer The only difference that I notice is the s.async=true; in… May 14, 2026 at 6:03 pm
  • Editorial Team
    Editorial Team added an answer Well, it seems I got this to work. I still… May 14, 2026 at 6:03 pm
  • Editorial Team
    Editorial Team added an answer I doubt that the "Minamoto" here is the real guy… May 14, 2026 at 6:03 pm

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.