Biorobot
Biorobot

Reputation: 3

What is the difference between the two different ways of importing Python widgets?

from PyQt5 import QtWidgets

and:

from PyQt5.QtWidgets import *

I am confused regarding this issue and until further notice I probably include too many imports.

Upvotes: 0

Views: 116

Answers (2)

eyllanesc
eyllanesc

Reputation: 244282

TL; DR; Don't use any of those ways to import


Both forms import all the elements of the QtWidgets module so they are considered bad practices.

When Python imports an element then it also imports all the sub-elements it contains, and more elements implies more memory consumption and more load time.

It is recommended to import only the classes, functions, etc. that are going to be used, for example from PyQt5.QtWidgets import QApplication.

Disclaimer: In many of my answers I use from PyQt5 import QtWidgets since all my answers are not necessarily intended to provide good practices but to solve a practical problem so I do not distract with extensive imports.

Upvotes: 1

Daniil Rose
Daniil Rose

Reputation: 123

RealPython has a real nice explanation here, but to summarize from PyQt5.QtWidgets import * isn't considered good practice in a large-scale production, as you are importing everything, which could accidentally override other important built-ins, and it may import more than you need. Stick with from PyQt5 import QtWidgets

Upvotes: 0

Related Questions