This issue tracker has been migrated to GitHub, and is currently read-only.
For more information, see the GitHub FAQs in the Python's Developer Guide.

classification
Title: Unexecuted import changes namespace
Type: behavior Stage: resolved
Components: Versions: Python 2.7
process
Status: closed Resolution: not a bug
Dependencies: Superseder:
Assigned To: Nosy List: benjamin.peterson, ezio.melotti, hippmr, michael.foord
Priority: normal Keywords:

Created on 2012-01-19 16:02 by hippmr, last changed 2022-04-11 14:57 by admin. This issue is now closed.

Files
File name Uploaded Description Edit
main.py hippmr, 2012-01-19 16:02 file that demonstrates the bug, needs follow-on over.py also
over.py hippmr, 2012-01-19 16:03 additional file need to run main.py
Messages (6)
msg151635 - (view) Author: Michael Hipp (hippmr) Date: 2012-01-19 16:02
A local *unexecuted* import appears to be changing the namespace. Attached files are ready to run.

# over.py
SOMETHING = "overridden"

# main.py
OVERRIDE = False
SOMETHING = "original"

def main():
    #global SOMETHING  # uncomment and it works
    if OVERRIDE:
        from over import SOMETHING  # comment out and it works
        pass
    print SOMETHING  # UnboundLocalError: local variable 'SOMETHING' referenced before assignment

The SOMETHING variable has a value from the module global namespace, but it gets lost due to an import that is never executed.

I would think an unexecuted statement shouldn't have any effect on anything.

The second file will have to be submitted in a follow-on, it appears
msg151636 - (view) Author: Michael Hipp (hippmr) Date: 2012-01-19 16:03
Add'l over.py file
msg151637 - (view) Author: Benjamin Peterson (benjamin.peterson) * (Python committer) Date: 2012-01-19 16:03
Not a bug. Basically, import is an explicit assignment statement.
msg151638 - (view) Author: Michael Hipp (hippmr) Date: 2012-01-19 16:05
Even an *unexecuted* import assignment statement?
msg151639 - (view) Author: Michael Foord (michael.foord) * (Python committer) Date: 2012-01-19 16:07
hippmr: the problem is that by importing SOMETHING inside that function you're creating a *local variable* called SOMETHING. If the override isn't executed, and SOMETHING isn't global, then that local variable doesn't exist - which is why you get that error.

So even if the import isn't executed, its existence in the function tells Python that name is local to the function.
msg151640 - (view) Author: Ezio Melotti (ezio.melotti) * (Python committer) Date: 2012-01-19 16:08
>>> OVERRIDE = False
>>> SOMETHING = "original"
>>> 
>>> def main():
...     if OVERRIDE:
...         SOMETHING = None
...     print SOMETHING
... 
>>> main()
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "<stdin>", line 4, in main
UnboundLocalError: local variable 'SOMETHING' referenced before assignment

http://docs.python.org/faq/programming.html#why-am-i-getting-an-unboundlocalerror-when-the-variable-has-a-value
History
Date User Action Args
2022-04-11 14:57:25adminsetgithub: 58035
2012-01-19 16:09:00ezio.melottisetstatus: open -> closed
resolution: not a bug
components: - None
stage: resolved
2012-01-19 16:08:34ezio.melottisetstatus: closed -> open

nosy: + ezio.melotti
messages: + msg151640

resolution: not a bug -> (no value)
stage: resolved -> (no value)
2012-01-19 16:07:00michael.foordsetstatus: open -> closed

nosy: + michael.foord
messages: + msg151639

resolution: not a bug
stage: resolved
2012-01-19 16:05:30hippmrsetstatus: closed -> open
resolution: not a bug -> (no value)
messages: + msg151638
2012-01-19 16:03:50benjamin.petersonsetstatus: open -> closed

nosy: + benjamin.peterson
messages: + msg151637

resolution: not a bug
2012-01-19 16:03:11hippmrsetfiles: + over.py

messages: + msg151636
2012-01-19 16:02:19hippmrcreate