trouble with reload

Colin J. Williams cjw at ncf.ca
Fri Aug 14 13:14:16 EDT 2009


Steven D'Aprano wrote:
> On Fri, 14 Aug 2009 09:23:17 -0400, Colin J. Williams wrote:
> 
>> It's typically a user module that needs to be reloaded.
> 
> What's a user module?
A module written by a user, as distinguished from a libary
> 
> 
>> It seems that  del sys.modules['moduleName'] has no effect.
> 
> sys.modules is just a dictionary, I find it hard to believe that deleting 
> from it has no effect. It works for me:
> 
>>>> import sys
>>>> import math
>>>> 'math' in sys.modules
> True
>>>> del sys.modules['math']
>>>> 'math' in sys.modules
> False
> 
> What behaviour do you get?
> 
> 
> Of course deleting the math module from the cache doesn't do anything to 
> the math module in your namespace:
> 
>>>> math
> <module 'math' from '/usr/lib/python2.5/lib-dynload/mathmodule.so'>
>>>> del math
>>>> math
> Traceback (most recent call last):
>   File "<stdin>", line 1, in <module>
> NameError: name 'math' is not defined
> 
> Of course deleting the module (or reloading it) doesn't have any effect 
> on any objects you already have:
> 
> 
>>>> import math
>>>> func = math.sin
>>>> del sys.modules['math']
>>>> del math
>>>> math.sin(1.2)
> Traceback (most recent call last):
>   File "<stdin>", line 1, in <module>
> NameError: name 'math' is not defined
>>>> func(1.2)
> 0.93203908596722629
> 
> 
> 
> 
>> Is there some other way of ensuring that any import goes to
>> moduleName.py, instead of moduleName.pyc?
> 
> Delete moduleName.pyc.
> 
> Make sure the .pyc file doesn't exist in the first place.
> 
> Make sure the last modification date of the .py file is newer than the 
> modification date of the .pyc file.

That's easier said than done, when one is working with an IDE.  The cached 
.pyc file might be different from that in the file.

Colin W.
> 
> 
> 



More information about the Python-list mailing list