variables bound in moudules are None when module is not completely imported

chrysn at chrysn at
Wed Feb 25 13:48:16 EST 2009

On Tue, Feb 24, 2009 at 03:27:19PM +0100, chrysn at wrote:
> * is there a workaround?
>     * especially, is there a workaround that works w/o rewriting the
>       modules that raise the exceptions? (otherwise, wrapping all the
>       stuff called in the __name__=="__main__" wrapper into a main()
>       function and then calling that would trivially solve that)

update: i've found one, but this only works if the exception is raised
at a point determined by the outside.

to explain why this is applicable: in the examples, i used `1/0` to
raise a zero division exception inside the module whose scope i want to
preserve. in my practical application, the exception is thrown by a
function that was previously prepared by the outside module and monkey
patched to where the inner module is expected to call a method from (in
my case, the gtk main loop).

now if the function raising the exception saves both
`sys._getframe().f_back.f_globals` and a .copy() of that dictionary, the
original dictionary can later (when the exception is caught and the
module's globals are all None) be .update()d with the copy, and the
original module globals are restored.

as said, this is just a workaround -- the original question still
remains open.


To use raw power is to make yourself infinitely vulnerable to greater powers.
  -- Bene Gesserit axiom
-------------- next part --------------
A non-text attachment was scrubbed...
Name: signature.asc
Type: application/pgp-signature
Size: 204 bytes
Desc: Digital signature
URL: <>

More information about the Python-list mailing list