Python Forum
UnboundLocalError, how to fix it please? - Printable Version

+- Python Forum (https://python-forum.io)
+-- Forum: Python Coding (https://python-forum.io/forum-7.html)
+--- Forum: General Coding Help (https://python-forum.io/forum-8.html)
+--- Thread: UnboundLocalError, how to fix it please? (/thread-7637.html)



UnboundLocalError, how to fix it please? - etrius - Jan-18-2018

Hello, i'm facing a problem with python, i'm actually using the 2.6 version one because the scripts have been made for it, the 3.4 kept talking about syntax error.....

Anyhow it seems that there is still a problem, i'm getting the following error :

Error:
File "C:\script\cmio.py", in line 25, in <module> File "C:\script\cmio.py", in line 24, in swig_import_helper UnboundLocalError: Local variable '_mod' referenced before assignment
Here the begining of the script, included the lines :

# This file was automatically generated by SWIG (http://www.swig.org).
# Version 1.3.39
#
# Do not make changes to this file unless you know what you are doing--modify
# the SWIG interface file instead.
# This file is compatible with both classic and new-style classes.

from sys import version_info
if version_info >= (2,6,0):
    def swig_import_helper():
        from os.path import dirname
        import imp
        fp = None
	_mod = None
        try:
            fp, pathname, description = imp.find_module('_cmio', [dirname(__file__)])
        except ImportError:
            import _cmio
            return _cmio
        if fp is not None:
            try:
                _mod = imp.load_module('_cmio', fp, pathname, description)
            finally:
                fp.close()
                return _mod
    _cmio = swig_import_helper()
    del swig_import_helper
else:
Any idea on how to fix this please? thanks


RE: UnboundLocalError, how to fix it please? - buran - Jan-18-2018

the indentation of this code doesn't look right, e.g lines 14-15.


RE: UnboundLocalError, how to fix it please? - nilamo - Jan-18-2018

_mod = None that doesn't look needed at all. You immediately define it right before returning it, so setting it to None at the top of the function doesn't do anything.

That said, it says it's been generated by SWIG. Why not just regenerate it?


RE: UnboundLocalError, how to fix it please? - etrius - Jan-18-2018

Edit : ok i will try, thanks


RE: UnboundLocalError, how to fix it please? - nilamo - Jan-18-2018

The last error was right before you imported _cmio. This error is saying _cmio is None, which means you didn't really fix the original error.

You shouldn't be editing this file. If it's not working, in it's original form, you should contact whoever wrote the package.