Here is a snippet of the code.
from PyQt6 import QtWidgets, QtCore, QtGui
from PyQt6.QtCore import QObject
from PyQt6.QtWidgets import QWidget # imports only QWidget
I do not see
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.
Can I do that in python ?
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.
Okay, thank you!
So basically using just “import” is pretty generic and when I need specific object I should use
“from”… "import " … myobject
Somehow I feel using just “import” is not very helpful.
It depends on your approach. For example you can call the bar
function from module foo like this:
Or like so:
from math import floor
Or you can do:
from math import floor as myfloor
They’re all useful under the right circumstances, it just depends on
what your situation as to how you want to manage the namespace.
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:
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
pd.blah2 to access various things from it,
because these emodules have a lot of top level names and the short names
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:
pd = pandas
from os.path import join
joinpath = join
without littering your namespace with the original
names - just the
Cameron Simpson firstname.lastname@example.org