imported module no longer available

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Jeff Dyke

    imported module no longer available

    I've come across an error that i'm not yet able to create a test case
    for but wanted to get see if someone could shed light on this.

    I have imported a module at the top of my file with
    import mymodulename

    this module is used many times in the current file successfully, but
    then I attempt to use it one more time and get: UnboundLocalErr or:
    local variable 'mymodulename' referenced before assignment

    if i add `print globals().keys( )` right before the line where the
    error occurs l see the module in the list (actual output, names
    changed to protect the innocent)
    ['HPADao', 'RDao', 'DriverBase', 'FKSUtility', 'PMDao',
    'mymodulename', 'IDriver', 'DriverTrack', 'HPPDao', 'setLogName',
    'HPDao', 'iparser', '__builtins__', '__file__', 'driver', '_LOGNAME',
    'sys', 'IClient', '__name__', 'copy', 'types', 'logging', 'iloader',
    'HPADao', '__doc__', 'PMDao', 'time', 'FormatLoad']
    Traceback (most recent call last):
    File "testunbound.py ", line 475, in <module>
    driver.getRData ('0605', 22528)
    File "testunbound.py ", line 256, in getRData
    print mymodulename

    the code that generates these two lines is:
    print globals().keys( )
    print mymodulename

    I can solve this by placing
    import mymodulename again in this method, but can't have that everywhere.

    I will try to come up with a generic reproduction of this issue, but
    that might take some time. I've also seen this in the past with the
    python builtin 'sys'
    I did see a bug http://bugs.python.org/issue2378, which has very
    similar behaviour, but is different.

    Python 2.5, ubuntu hardy

    Thanks for any insight,
    Jeff
  • Mark Tolonen

    #2
    Re: imported module no longer available


    "Jeff Dyke" <jeff.dyke@gmai l.comwrote in message
    news:mailman.40 6.1216648575.92 2.python-list@python.org ...
    I've come across an error that i'm not yet able to create a test case
    for but wanted to get see if someone could shed light on this.
    >
    I have imported a module at the top of my file with
    import mymodulename
    >
    this module is used many times in the current file successfully, but
    then I attempt to use it one more time and get: UnboundLocalErr or:
    local variable 'mymodulename' referenced before assignment
    >
    if i add `print globals().keys( )` right before the line where the
    error occurs l see the module in the list (actual output, names
    changed to protect the innocent)
    ['HPADao', 'RDao', 'DriverBase', 'FKSUtility', 'PMDao',
    'mymodulename', 'IDriver', 'DriverTrack', 'HPPDao', 'setLogName',
    'HPDao', 'iparser', '__builtins__', '__file__', 'driver', '_LOGNAME',
    'sys', 'IClient', '__name__', 'copy', 'types', 'logging', 'iloader',
    'HPADao', '__doc__', 'PMDao', 'time', 'FormatLoad']
    Traceback (most recent call last):
    File "testunbound.py ", line 475, in <module>
    driver.getRData ('0605', 22528)
    File "testunbound.py ", line 256, in getRData
    print mymodulename
    >
    the code that generates these two lines is:
    print globals().keys( )
    print mymodulename
    >
    I can solve this by placing
    import mymodulename again in this method, but can't have that everywhere.
    >
    I will try to come up with a generic reproduction of this issue, but
    that might take some time. I've also seen this in the past with the
    python builtin 'sys'
    I did see a bug http://bugs.python.org/issue2378, which has very
    similar behaviour, but is different.
    >
    Python 2.5, ubuntu hardy
    >
    Thanks for any insight,
    Jeff
    That error will occur if somewhere in a function or method you accidently
    assign another value to a global variable, without declaring it with the
    global keyword first, even if it occurs later in the function. Example:

    import os
    print os.getcwd()
    def func():
    print globals().keys( )
    print os
    os=1
    func()

    OUTPUT:

    c:\
    ['__builtins__', '__file__', 'func', '__name__', 'os', '__doc__']
    Traceback (most recent call last):
    File
    "C:\dev\python\ Lib\site-packages\python win\pywin\frame work\scriptutil s.py",
    line 414, in ImportFile
    exec codeObj in __main__.__dict __
    File "<auto import>", line 1, in <module>
    File "test.py", line 10, in <module>
    func()
    File "test.py", line 7, in func
    print os
    UnboundLocalErr or: local variable 'os' referenced before assignment

    Check in the function that caused the error to see if later in the code the
    module name is being used as a local variable.

    --Mark

    Comment

    Working...