New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Unexecuted import in function causes UnboundLocalError #79250
Comments
Having 'import logging.config' in an if statement in a function causes a namespace issue, despite the fact that the import is not reached. Example code: --- # Test weird import bug import logging
config = {}
config['log'] = {}
config['log']['log_type'] = 'file'
config['log']['log_file'] = './log'
config['log']['config'] = { 'version' : 1 }
def do_config_logging():
if config['log']['log_type'] == 'from_config':
import logging.config
logging.config.dictConfig(config['log']['config'])
elif config['log']['log_type'] == 'file':
logging.basicConfig(filename=config['log']['log_file'])
logging.info("start logging")
if __name__ == "__main__":
do_config_logging() This results in: Traceback (most recent call last):
File "./bug.py", line 25, in <module>
do_config_logging()
File "./bug.py", line 20, in do_config_logging
logging.basicConfig(filename=config['log']['log_file'])
UnboundLocalError: local variable 'logging' referenced before assignment Notes: This was run on Ubuntu Linux 18.04 Intel 64-bit, Python version 3.6.6 The problem does not occur if the branch is actually taken, and it does not occur if the 'if' statement is not in a function. It also does not occur if 'logging.config' is imported as some other name, eg. 'configlogging'. virtualenv is installed (via the distribution package) but not in use in the test case. |
Could you share a pastebin? Thank you
>
>
> This results in:
>
> Traceback (most recent call last):
> File "./bug.py", line 25, in <module>
> do_config_logging()
> File "./bug.py", line 20, in do_config_logging
> logging.basicConfig(filename=config['log']['log_file'])
> UnboundLocalError: local variable 'logging' referenced before assignment
>
>
> Notes:
>
> This was run on Ubuntu Linux 18.04 Intel 64-bit, Python version 3.6.6
>
> The problem does not occur if the branch is actually taken, and it does not occur if the 'if' statement is not in a function. It also does not occur if 'logging.config' is imported as some other name, eg. 'configlogging'.
>
> virtualenv is installed (via the distribution package) but not in use in the test case.
>
>
|
Sure, it's at https://pastebin.com/L1RMPD7K -James On 10/25/2018 12:30 PM, Stéphane Wirtel wrote:
>>
>>
>> This results in:
>>
>> Traceback (most recent call last):
>> File "./bug.py", line 25, in <module>
>> do_config_logging()
>> File "./bug.py", line 20, in do_config_logging
>> logging.basicConfig(filename=config['log']['log_file'])
>> UnboundLocalError: local variable 'logging' referenced before assignment
>>
>>
>> Notes:
>>
>> This was run on Ubuntu Linux 18.04 Intel 64-bit, Python version 3.6.6
>>
>> The problem does not occur if the branch is actually taken, and it does not occur if the 'if' statement is not in a function. It also does not occur if 'logging.config' is imported as some other name, eg. 'configlogging'.
>>
>> virtualenv is installed (via the distribution package) but not in use in the test case.
>>
>>
|
This is expected behaviour: import is a form of assignment. "import logging", like "logging = 1", tells the compiler to treat logging as a local variable (unless you declare logging as global). As the exception says, you are trying to access the logging local variable before it has been assigned to. You can (and should!) give a SHORT and SIMPLE demonstration, without any excess and irrelevant code: py> def demo():
... if False:
... import logging
... logging
...
py> demo()
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "<stdin>", line 4, in demo
UnboundLocalError: local variable 'logging' referenced before assignment So this is expected behaviour. |
Stéphane, I'm curious why you asked for a pastebin when James already provided the code right here in the tracker? (Your email even included a copy of that code.) Why split the information into another website? |
I don't quite follow... the 'import logging.config' statement should -James On 10/25/2018 12:58 PM, Steven D'Aprano wrote:
>
> Steven D'Aprano <steve+python@pearwood.info> added the comment:
>
> This is expected behaviour: import is a form of assignment.
>
> "import logging", like "logging = 1", tells the compiler to treat logging as a local variable (unless you declare logging as global). As the exception says, you are trying to access the logging local variable before it has been assigned to.
>
> You can (and should!) give a SHORT and SIMPLE demonstration, without any excess and irrelevant code:
>
> py> def demo():
> ... if False:
> ... import logging
> ... logging
> ...
> py> demo()
> Traceback (most recent call last):
> File "<stdin>", line 1, in <module>
> File "<stdin>", line 4, in demo
> UnboundLocalError: local variable 'logging' referenced before assignment
>
> So this is expected behaviour.
>
>
|
Yes, that's exactly right. That's how local variables work in Python: x = 999 # global x
def demo():
if False:
x = 1
x # local x has no value does the same thing. This is standard, documented behaviour, regardless |
So just the fact that somewhere in the function a name is referenced, I guess the moral is, pay attention to scope when importing submodules Thanks for looking at this, sorry it wasn't a bit more interesting :) -James On 10/25/2018 01:32 PM, Steven D'Aprano wrote:
|
Hi Steven, Sure, I am going to explain, I was at the social event of PyCon Germany without my laptop and the code was not really clear on my smartphone. I wanted to know if there was an indentation issue. It's also the reason for my big reply with the initial email because I have replied on my phone. But after that, you have replied. I am really sorry for the inconvenience. Thank you |
Note: these values reflect the state of the issue at the time it was migrated and might not reflect the current state.
Show more details
GitHub fields:
bugs.python.org fields:
The text was updated successfully, but these errors were encountered: