reloading the module imported as 'from ... import ...'

Piet van Oostrum piet at cs.uu.nl
Mon Aug 10 11:00:34 EDT 2009


>>>>> Steven D'Aprano <steven at REMOVE.THIS.cybersource.com.au> (SD) wrote:

>SD> On Sun, 09 Aug 2009 22:48:31 -0700, AlF wrote:
>>> Steven D'Aprano wrote:
>>>> On Sun, 09 Aug 2009 20:43:41 -0700, AlF wrote:
>>>> 
>>>>> Hi,
>>>>> 
>>>>> what is the best way to reload the module imported using 'from ...
>>>>> import ...'
>>>> 
>>>> 
>>>> Have you tried "from ... import ..." again?
>>>> 
>>>> 
>>> I have not because of an assumption that "import" imports the module
>>> just once. 

>SD> Ah, of course the cached module will still reflect the older version. 
>SD> Sorry, I was thinking about solving a different problem:

If you do a reload in between the cached version is replaced by the new
version. However, objects from the old module that have references
before the reload will still lie around. This could cause very subtle
bugs.

Python 2.6.2 (r262:71600, Apr 16 2009, 09:17:39) 
[GCC 4.0.1 (Apple Computer, Inc. build 5250)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>> import testmod
>>> from testmod import TestClass
>>> a = TestClass()
>>> b = TestClass()
>>> a.__class__ is b.__class__
True
>>> reload(testmod)
<module 'testmod' from 'testmod.pyc'>
>>> c = TestClass()
>>> c.__class__ is a.__class__
True
>>> from testmod import TestClass
>>> d = TestClass()
>>> d.__class__ is a.__class__
False

-- 
Piet van Oostrum <piet at cs.uu.nl>
URL: http://pietvanoostrum.com [PGP 8DAE142BE17999C4]
Private email: piet at vanoostrum.org



More information about the Python-list mailing list