Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Python: `from x import *` not importing everything

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?

like image 431
varesa Avatar asked Nov 09 '13 22:11

varesa


2 Answers

If shaders is a submodule and it’s not included in __all__, from … import * won’t import it.

And yes, it is a submodule.

like image 127
rninty Avatar answered Sep 26 '22 11:09

rninty


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:

from OpenGL.GL import shaders

Or, if you only want to have a few functions of shaders:

from OpenGL.Gl.shaders import function1, function2, function3

And if you want to have all the functions of shaders, use:

from OpenGL.Gl.shaders import *

Hope this helps!

like image 21
aIKid Avatar answered Sep 23 '22 11:09

aIKid