I encountered this problem while writing a tool that I intended to search a Python module and/or package for a given object. The script is interesting and worth another post all on its own. The gist: you provide the script with two arguments, the module/package to search, and a term to search for. The script should then give you a listing of all the places it found an object containing your search term.
I was testing my script with various terms I tend to use in PyQt4 applications
1 2 3 4 5
python import_searcher.py -s MinimumExpanding PyQt4.Qt.QSizePolicy.MinimumExpanding = 3 PyQt4.QtGui.QSizePolicy.MinimumExpanding = 3 PyQt4.Qwt5.qplt.QSizePolicy.MinimumExpanding = 3
Notice anything odd about the above output? Looks like
shows up in two almost identical locations --
Naturally, I thought there was a bug in my script, but after some debugging and
reading I found the following jewel on the
PyQt4 Wikipedia page:
The Qt module consolidates the classes contained in all of the modules described above into a single module. This has the advantage that you don't have to worry about which underlying module contains a particular class. It has the disadvantage that it loads the whole of the Qt framework, thereby increasing the memory footprint of an application. Whether you use this consolidated module or the individual component modules is down to personal taste.
For example, assume that your application is only using the
PyQt4.QtGui module, and you mistakenly decide that you need something from
PyQt4.Qt. This single import could essentially double your memory usage :
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25
Line # Mem Usage Increment Line Contents ================================================ 1 @profile 2 def import_qt_module_by_module(): 3 #from PyQt4 import QtCore 4 #from PyQt4 import QtDBus 5 6.953 MB 0.000 MB #from PyQt4 import QtDeclarative 6 12.844 MB 5.891 MB from PyQt4 import QtGui 7 #from PyQt4 import QtHelp 8 #from PyQt4 import QtMultimedia 9 #from PyQt4 import QtNetwork 10 #from PyQt4 import QtOpenGL 11 #from PyQt4 import QtScript 12 #from PyQt4 import QtScriptTools 13 #from PyQt4 import QtSql 14 #from PyQt4 import QtSvg 15 #from PyQt4 import QtTest 16 #from PyQt4 import QtWebKit 17 #from PyQt4 import QtXml 18 #from PyQt4 import QtXmlPatterns 19 #from PyQt4 import phonon 20 #from PyQt4 import QtAssitant 21 #from PyQt4 import QtDesigner 22 #from PyQt4 import QtAxContainer 23 19.387 MB 6.543 MB from PyQt4 import Qt
Note that just importing 'PyQt4.Qt' increased the application memory usage by 6.543 MB. This could be costly depending on how much memory your application was already using and how much your system has available. While this situation deals with smaller figures, multiple unnecessary imports can easily compound, leading to more significant memory usage issues.
Be careful of what you import. It could be redundant, costly, or both.
Read the documentation carefully. The main PyQt4 documentation alludes to this redundant
PyQt4.Qt module by describing it with the following, "Consolidates all other modules into a single module for ease of use at the expense of memory."
Remember to try stuff, play around, and have some fun coding. A one-off script can lead to a nice, useful discovery.