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 101231
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 11, 20262026-05-11T00:43:53+00:00 2026-05-11T00:43:53+00:00

I ran across this case of UnboundLocalError recently, which seems strange: import pprint def

  • 0

I ran across this case of UnboundLocalError recently, which seems strange:

import pprint  def main():     if 'pprint' in globals(): print 'pprint is in globals()'     pprint.pprint('Spam')     from pprint import pprint     pprint('Eggs')  if __name__ == '__main__': main() 

Which produces:

pprint is in globals() Traceback (most recent call last):   File 'weird.py', line 9, in <module>     if __name__ == '__main__': main()   File 'weird.py', line 5, in main     pprint.pprint('Spam') UnboundLocalError: local variable 'pprint' referenced before assignment 

pprint is clearly bound in globals, and is going to be bound in locals in the following statement. Can someone offer an explanation of why it isn’t happy resolving pprint to the binding in globals here?

Edit: Thanks to the good responses I can clarify my question with relevant terminology:

At compile time the identifier pprint is marked as local to the frame. Does the execution model have no distinction where within the frame the local identifier is bound? Can it say, ‘refer to the global binding up until this bytecode instruction, at which point it has been rebound to a local binding,’ or does the execution model not account for this?

  • 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. 2026-05-11T00:43:54+00:00Added an answer on May 11, 2026 at 12:43 am

    Looks like Python sees the from pprint import pprint line and marks pprint as a name local to main() before executing any code. Since Python thinks pprint ought to be a local variable, referencing it with pprint.pprint() before ‘assigning’ it with the from..import statement, it throws that error.

    That’s as much sense as I can make of that.

    The moral, of course, is to always put those import statements at the top of the scope.

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

Sidebar

Related Questions

I ran across this code recently which is part of a template class: operator
I recently ran across this puzzle, was finally able to struggle out a hacky
Ran across this recently and wondering if someone out there can give me a
I recently ran across this problem while trying to implement a service that has
I recently ran across this code in one of the projects I'm working on,
I ran across this bit of code and to me it seems like its
I recently ran across some 3rd party C# code which does the following: public
So I ran across this recently: http://www.nicalis.com/ And I was curious: Is there a
I recently ran across a set of code which instantiated local maps as following:
I ran across this problem, which has been driving me nuts. In a nutshell,

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.