from PyQt6 import QtWidgets, QtCore, QtGui
from PyQt6.QtCore import QObject
from PyQt6.QtWidgets import QWidget # imports only QWidget
I do not see
“import PyQy6”
anywhere ahead of this code.
Should there be such code somewhere ?
My second question
These “imports” are necessary in almost all packages in the project. In “C” I build “common #include” and
add #include common_include to each file.
In from PyQt6 import QtWidgets, QtCore, QtGui it’s telling Python to import some names from module PyQt6. You don’t need to tell it to import PyQt6 itself because it already knows that it needs to import that module in order to import those names from it.
As for the second question, you could put those imports in another module, say, common, and then do from common import *, but it’s clearer just to leave it as it is.
import makes a name from a module (or the name of a module)
available to your code. That’s its whole purpose.
For a lot of code you’ll see:
from module_name import name1, name2, ...
where your code uses specific things. These lines grow incrementally as
you use more names as the code grows.
Less common is:
import module_name
because then you need to use module_name.name1 and so forth to access
things from it. However, if you’re using a lot of ad hoc names from a
module, this can be the convenient thing. For example, it is very
common in the scientific computing world to:
import numpy as np
import pandas as pd
and then use np.blah and pd.blah2 to access various things from it,
because these emodules have a lot of top level names and the short names np and pd are well known conventions for these particular imports,
entirely to make the module_name.name incantation short and easily to
type and read.
Finally, sometimes we import a very generic name as a less generic name
for clarity. For example I often:
from os.path import join as joinpath
so that’s it’s clear that I’m using the “join” for filesystem paths in
my code. Effectively the as form of import is like doing this: