imported module no longer available

Jeff Dyke jeff.dyke at gmail.com
Mon Jul 21 12:31:12 EDT 2008


my apologies, to Fredrick, my response when solely to him.  reply
below, hopefully keeping thread intact.

On Mon, Jul 21, 2008 at 12:28 PM, Jeff Dyke <jeff.dyke at gmail.com> wrote:
> On Mon, Jul 21, 2008 at 10:19 AM, Fredrik Lundh <fredrik at pythonware.com> wrote:
>> Jeff Dyke wrote:
>>
>>> 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
>>
>> Let me guess: you've done
>>
>>    def myfunc():
>>        print mymodulename
>>        import mymodulename
>
> actually no, the only things in that fucntion were.
>        print globals().keys() - i see it here
>        print mymodulename - it fails here.
>
> the `import mymodulename` statement is at the very top of the file.
>
> plus the processing that was attempted after.  in fact in the calling
> method i was able to execute print mymodulename and it printed the
> expected python output.
>
>>
>> or something similar?  Getting an exception in this case is the excepted
>> behaviour; the reason being that a variable in a block only belongs to a
>> single scope. for the entire block.  For details, see:
>>
>>    http://docs.python.org/ref/naming.html
>>
>> Especially this section:
>>
>> "If a name binding operation occurs anywhere within a code block, all uses
>> of the name within the block are treated as references to the current block.
>> This can lead to errors when a name is used within a block before it is
>> bound. This rule is subtle. Python lacks declarations and allows name
>> binding operations to occur anywhere within a code block. The local
>> variables of a code block can be determined by scanning the entire text of
>> the block for name binding operations."
>>
>> To fix this, mark the name as global:
>>
>>    def myfunc():
>>        global mymodulename # I mean the global name!
>>        print mymodulename
>>        import mymodulename
>
>
> So i went back to check that the name 'mymodulename' was not getting
> overwritten by something else and the error went away.  I've been
> working on something else entirely for the past few hours and have
> changed none of the code...and now it works.  which is even more
> troublesome then the error itself.
>
> Follow on question.  If this name, mymodulename, was imported in some
> other module.fucntion local to a function like
> def anotherfunc():
>    import mymodulename
>
> would that remove it from the globals() and save it to a locals() ?  I
> would assume the answer to be no.
>
> Thanks for you response.
> Jeff
>
>
>>
>> </F>
>>
>> --
>> http://mail.python.org/mailman/listinfo/python-list
>>
>



More information about the Python-list mailing list