[PyQt] import speed overhead question

Phil Thompson phil at riverbankcomputing.co.uk
Thu Nov 22 00:10:49 GMT 2007


On Wednesday 21 November 2007, Linos wrote:
> Hello all,
> 	i have a question about speed optimization with PyQt, i cant find any
> clear explanation about this question in google, i am developing an app
> with many .py files, in the beginning i am using this imports.
>
> from PyQt4.QtCore import *
> from PyQt4.QtGui import *
> from PyQt4.QtSql import *
>
> to have no problem using classes but i would like to post any questions i
> suppose any people on the list have to know:
>
> 1- if i import a file and this file import the same, it is imported two
> times wasting the same time in the same imports other time?
>
> main.py
> 	from PyQt4.QtCore import *
> 	from PyQt4.QtGui import *
> 	from PyQt4.QtSql import *
> 	import testdlg
>
> testdlg.py
> 	from PyQt4.QtCore import *
> 	from PyQt4.QtGui import *
> 	from PyQt4.QtSql import *

Two things happen when importing...

The very first time a module is imported it is parsed and executed (if it is a 
pure Python module) or loaded and initialised (if it is a C/C++ extension 
module).

Every time a module is imported the objects you have asked to be imported are 
placed in the importing module's dictionary.

> 2- i have changed any files to import like this:
>
> from PyQt4.QtCore import QRegExp, QString, Qt, SIGNAL, SLOT, QVariant,
> QChar from PyQt4.QtGui import QLabel, QDialog, QLineEdit, QDialogButtonBox,
> QGridLayout, QMessageBox, QApplication, QIcon, QInputDialog
> from PyQt4.QtSql import QSqlQuery
>
> i know other people (and pyuic4) use this :
>
> from PyQt4 import QtCore
>
> and later use ever first QtCore in classes names "QtCore.QString" but i
> dont like to write too much unnecessary code when i know i will not have
> name collision, any other way to speed up the imports?

Import speed increases and memory usage decreases as you go down the following 
list...

from PyQt4.QtCore import *
from PyQt4.QtCore import QString, Qt, etc
from PyQt4 import QtCore

In terms of increasing speed during execution the order is...

from PyQt4 import QtCore
from PyQt4.QtCore import *
from PyQt4.QtCore import QString, Qt, etc

> 3- is this really important to speed up the application?

No.

Phil


More information about the PyQt mailing list