What does "from modulox import" really mean *"

As far as I know this matters all the classes and functions of a certain file without having to reference it in the code, right? But I'm starting to think that from modulo import * doesn't mean that.

I was studying the Tkinter library and 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 colorchooser again? Actually apparently the colorchooser was not even imported.

If I try to use the colorchooser without using the second line it simply says that it is not defined. Why do you need to import colorchooser twice?

Author: hkotsubo, 2020-03-19

2 answers

You don't matter twice. Using the asterisk does not necessarily mean that you will import all available classes, functions, or variables within a module. There are two cases...

  1. Variable __all__ in File __has not been set init__.py , hence Python will load whatever is set inside (which may not be the best of behaviors), or

  2. Import everything that is considered necessary and that was set within __all__, which gives you much more control over what's being loaded.

For example you have a module called modulo and in its directory there are two files, one containing the classes, the somas.py :

class Media:
    ...

class Soma:
    ...

And the other __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 can't use the Class Soma because only Media it was loaded automatically.

Being necessary to load it manually (in this case only from the Class Soma):

>>> from teste import Soma
>>> b = Soma()

Or, depending on the case, adding it within __all__.

 6
Author: Giovanni Nunes, 2020-03-19 00:27:44

Using the asterisk (*) will instead import all the data (variables, functions and classes) from the module you specified in from, for no other purpose.

What happens, is that by doing from tkinter import *, you are importing from the package tkinter the file __init__.py. And unlike what you think, the colorchooser it is not inside the file you import, as it is another module inside the package.

Soon, if you only perform the first import, you will not be able to access the colorchooser and consequently a NameError will be generated.

from tkinter import *             # Aqui você importa tudo do __init__.py
from tkinter import colorchooser  # Aqui você importa o módulo colorchooser.py do pacote

You can prove that the colorchooser is a module using the function help.

>>> help(colorchooser)
Help on module tkinter.colorchooser in tkinter:

NAME
    tkinter.colorchooser
...

See here the contents of the package. You can also view the content by going to the <python_path>/lib/tkinter directory or by running help(tkinter) to look at the documentation.

 5
Author: JeanExtreme002, 2020-03-19 00:50:00