I know that import *
is bad, but I sometimes use it for quick prototyping when I feel too lazy to type or remember the imports
I am trying the following code:
from OpenGL.GL import *
shaders.doSomething()
It results in an error: `NameError: global name 'shaders' is not defined'
If I change the imports:
from OpenGL.GL import *
from OpenGL.GL import shaders
shaders.doSomething()
The error disappears. Why does *
not include shaders
?
I learned this from my own situation. A module did not automatically import along with the rest of the package. Before that experience my mistaken understanding was that every packages's modules automatically import from an
import x
or afrom x import *
. They don't.Beginners might expect EVERYTHING to import under those calls, I believe. But the following GUI programming code, which is common, demonstrates that that's not the case:
In the above example, module
ttk
doesn't import automatically along with the rest of thetkinter
package, for instance.The explanation that I've been told is as follows: when you use
from x import *
, you actually only imported things inyour-python-location/lib/x/__init__.py
Packages are folders. Modules are files. If the import calls for specific files then the package folder's
__init_.py
will enumerate the specific files to import.If
shaders
is a submodule and it’s not included in__all__
,from … import *
won’t import it.And yes, it is a submodule.
shaders
is a submodule, not a function.The syntax
from module import something
doesn't import submodules (Which, as another answer stated, not defined in__all__
).To take the module, you'll have to import it specifically:
Or, if you only want to have a few functions of
shaders
:And if you want to have all the functions of
shaders
, use:Hope this helps!