Is there any way to unimport a library

Gelonida N gelonida at gmail.com
Sun Nov 20 10:15:05 EST 2011


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.


My usecase is following:


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
if not success:
    . . .



Basically if I am not amble to import lib1_version1 AND lib2_version1,
then I wanted to make sure, that lib1_version1 does not waste any memory


At this moment this is more a thought excercise than a real issue, but I
thought that perhaps somebody encountered this kind of issue and had an
idea how to deal with such situations.

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.




More information about the Python-list mailing list