Is there any way to unimport a library

Gelonida N gelonida at gmail.com
Sun Nov 20 11:39:28 EST 2011


Steven, Mika,

Thanks for your answers.

It's always good to know which options exist.
It makes it easier to choose the right one depending on the situation.

On 11/20/2011 04:46 PM, Steven D'Aprano wrote:
> On Sun, 20 Nov 2011 16:15:05 +0100, Gelonida N wrote:
> 
>> I wondered whether there is any way to un-import a library, such, that
>> it's occupied  memory and the related shared libraries are released.
> 
> 
> You can remove it from your application's namespace:
> 
> import math
> result = math.sin(1.2345)
> del math
> 
> 
> This, however, is not sufficient to free the module, as it is cached. You 
> can (but shouldn't!) do this as well:
> 
> import sys
> del sys.modules['math']
> 
> 
> The problem is, if you delete the module from the cache, but some other 
> part of your application still holds onto a reference to the module 
> (directly, or indirectly), the next time you import the library, Python 
> will not use the cached version but will create a second, independent 
> copy of the module in memory. Instead of saving memory, you have just 
> DOUBLED the amount of memory used by the library. You may also run into 
> bizarre, hard to debug problems due to breaking the "modules are 
> singletons" promise.
> 
in my case only one module (the one with the try import statements)
would import these libraries.


>> success = False
>> try:
>>     import lib1_version1 as lib1
>>     import lib2_version1 as lib2
>>     success = True
>> except ImportError:
>>     pass
>> if not success:
>>     try:
>>         import lib1_version2 as lib1
>>         import lib2_version2 as lib2
>>         success = True
>>     except importError:
>>         pass
> 
> 
> Can you mix lib1_version1 and lib2_version2? If so, the best way of doing 
> this would be:
> 
> try:
>     import lib1_version1 as lib1
> except ImportError:
>     try:
>         import lib1_version2 as lib1
>     except ImportError:
>         # Last chance. If this fails, it is a fatal error.
>         import lib1_version3 as lib1
> 
No mixing would not be possible.

So either I need the first two libs or the second two.

> 
>> One solution, that I could imagine is running the program a first time,
>> detect all existing libraries and write out a config file being use the
>> next time it is run, such, that immediately the right libs are imported.
> 
> What if the libs change between one run of your program and the next?
>

Well that's why wondered whether there is a dynamic solution.
However if I'd like to force a rescan I could always just delete the
config file.


I think I'll look at imp.find_module as Miki suggested.
In my case it should probably be enough to know whether a group of
modules exist.
Theoretically a module could exist, but fail to import but if in this
rare situations one of the libraries stays in memory it wouldn't be the
biggest issue.

I'd just like to avoid that (this is not my use case, but just a (crazy)
example) TKInter, wxWidgets, and pyQt were all loaded even if due I'd
use only one of them due to the lack of other packages








More information about the Python-list mailing list