As far as I know this imports all classes and functions from a certain file without having to reference it in code, right? But I'm starting to think that
from modulo import * doesn't mean that.
I was studying the tkinter library and I came across the following situation:
from tkinter import * from tkinter import colorchooser
If in the first
import you already used
* , why would you need to import the
colorchooser again? Actually apparently the
colorchooser wasn't even imported.
If I try to use
colorchooser without using the second line it just says it's not set. Why do you need to import
You don't matter twice. Using the asterisk does not necessarily mean that you will import all classes, functions or variables available within a module. There we have two cases…
__all__variable was not defined in __init__.py file, so Python will load whatever is defined in there (which can't be the best behavior), or
Import everything that is deemed necessary and that has been defined inside
__all__, which gives you much more control over what is being loaded.
For example you have a module called module and in its directory there are two files, one containing the classes, somas.py :
class Media: ... class Soma: ...
And the other is __init__.py which even indicates that this directory is a module:
__all__ = ("Media",) from .somas import Media, Soma
In this scenario if you do:
>>> from modulo import * >>> a = Media() >>> b = Soma() Traceback (most recent call last): File "<stdin>", line 1, in <module> NameError: name 'Soma' is not defined
You cannot use the
Soma class as only
Media was automatically loaded.
It is necessary to load it manually (in this case only the
>>> from teste import Soma >>> b = Soma()
Or, depending on the case, by appending it inside