imported module no longer available

Jeff Dyke jeff.dyke at gmail.com
Mon Jul 21 09:56:03 EDT 2008


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: UnboundLocalError:
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



More information about the Python-list mailing list