<div dir="ltr">Correction: even Python modules cannot be unloaded, not even in Python 3.5. Multiple discussions on the web indicate this, and a simple Python script with a utility that shows memory consumption (like process explorer on Windows) shows it. Here I tried with a pure-Python package called pytest:<div><br></div><blockquote style="margin:0 0 0 40px;border:none;padding:0px"><div><div>import time, sys, gc</div></div><div><div><br></div></div><div><div>time.sleep(20)</div></div><div><div><br></div></div><div><div># save modules catalog and import a big module</div></div><div><div>old_modules = sys.modules.copy()</div></div><div><div>import pytest # will cause 16 megs increase in process mem</div></div><div><div>print(sys.modules)</div></div><div><div>time.sleep(10)</div></div><div><div><br></div></div><div><div># cleanup and restore original catalog to ensure that neither pytest or any of the many mofulrd it </div></div><div><div># imported are still referenced anywhere, and get gc to cleanup</div></div><div><div>del pytest</div></div><div><div>sys.modules = old_modules</div></div><div><div>del old_modules</div></div><div><div>gc.collect()   # mem stays at 16 megs more than original</div></div><div><div>print(sys.modules)</div></div><div><div>time.sleep(10)</div></div></blockquote><div><br></div><div>Looks like there might be another attempt at supporting pure-Python module unloading by developers of CPython but it is not clear when, and to what extent it would work (you can imagine that modules would have to be marked for how many times their symbols are referenced because two modules A and B might import a common module C -- unloading A should not unload C if B is present, but then unloading B after A unloaded should also unload C). </div><div><br></div><div>One post on python dev bug tracker site confirms that unloading a C/C++ extension (DLL) is near impossible. </div><div>Oliver</div><div><br></div></div><div class="gmail_extra"><br clear="all"><div><div class="gmail_signature" data-smartmail="gmail_signature"><div dir="ltr"><div><div dir="ltr"><div dir="ltr"><div><font size="2" style="font-size:small">Oliver</font><div style="font-size:small"><font size="1">Open Source contributions: <a href="http://pubsub.sf.net/" style="color:rgb(17,85,204)" target="_blank">PyPubSub</a>,</font><span style="font-size:x-small"> </span><a href="https://github.com/schollii/nose2pytest" style="color:rgb(17,85,204);font-size:x-small" target="_blank">nose2pytest</a><span style="font-size:x-small">,</span><span style="font-size:x-small"> L</span><a href="http://lua-icxx.sf.net/" style="font-size:x-small;color:rgb(17,85,204)" target="_blank">ua-iCxx</a><span style="font-size:x-small">, </span><a href="http://iof.sf.net/" style="font-size:x-small;color:rgb(17,85,204)" target="_blank">iof</a></div><div style="font-size:small"><font size="1"><a href="http://stackoverflow.com/users/869951/schollii" style="color:rgb(17,85,204)" target="_blank">StackOverflow</a> contributions</font></div></div><div><font size="1"><br></font></div><div></div></div></div></div></div></div></div>
<br><div class="gmail_quote">On Wed, Sep 7, 2016 at 9:48 PM, oliver <span dir="ltr"><<a href="mailto:oliver.schoenborn@gmail.com" target="_blank">oliver.schoenborn@gmail.com</a>></span> wrote:<br><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex"><span class=""><br><br><div class="gmail_quote"><div dir="ltr">On Wed, Sep 7, 2016, 19:14 Andreas Pakulat <<a href="mailto:apaku@gmx.de" target="_blank">apaku@gmx.de</a>> wrote:<br></div><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex">Hi,<br>
<br>
On Wed, Sep 7, 2016 at 11:57 PM, Xavion <<a href="mailto:xavion.0@gmail.com" target="_blank">xavion.0@gmail.com</a>> wrote:<br>
> So, you're both saying that the following two lines are equivalent (from a<br>
> memory footprint standpoint):<br>
><br>
> from PyQt5.QtCore import QVariant<br>
> from PyQt5 import QtCore<br>
><br>
> In other words, in both cases, the whole of 'QtCore' will be imported<br>
> (rather than just 'QVariant' in the first case).</blockquote></div><div><br></div></span><div>That is correct and same for all python packages/modules not just pyqt </div><div class="gmail_quote"></div><div><br></div><div class="gmail_quote"><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex"><span class="">
> Florian: I think you're saying that I can reduce the memory usage by doing<br></span>
> the following...<span class=""><br>
><br>
> from PyQt5 import QtCore<br>
> mVariant = QtCore.QVariant<br>
> del QtCore<br></span></blockquote></div><div><br></div><div>Python modules can be unloaded this way but I don't think a DLL gets unloaded from memory once it is no longer in use by an app. But even if there was code in the Python interpreter to unload a DLL it would not get used because the above code will still be holding a reference to a symbol that is defined in the DLL so the DLL will not be considered unlovable. </div><div><br></div><div>Why are you so concerned with memory occupied by modules? </div></blockquote></div><br></div>