This import statement:
from tkinter import *
does not import tkinter.filedialog. Why it doesn't?
This import statement:
from tkinter import *
does not import tkinter.filedialog. Why it doesn't?
tkinter is a package, when doing from tkinter import * , it would import all the names defined in the __init__.py for the tkinter package, as well as only modules and subpackages defined in the __all__ in the __init__.py of tkinter package.
In my Python 3.4 , there is no __all__ defined in tkinter/__init__.py , hence it does not import any modules (like filedialog) from within it.
This is explained in the documentation -
The only solution is for the package author to provide an explicit index of the package. The import statement uses the following convention: if a package’s
__init__.pycode defines a list named__all__, it is taken to be the list of module names that should be imported when from package import * is encountered.If
__all__is not defined, the statement from sound.effects import * does not import all submodules from the package sound.effects into the current namespace; it only ensures that the package sound.effects has been imported (possibly running any initialization code in__init__.py) and then imports whatever names are defined in the package. This includes any names defined (and submodules explicitly loaded) by__init__.py.
Generally, the values imported from a from <package> import * depend on the values specified in the __all__ list for the __init__ file of that package.
Not being able to import filedialog means that it is not contained in the __all__ list for the tkinter __init__ file.
A quick way to evaluate whether a package 'exports' some submodules is to evaluate whether it has an __all__ attribute after you import it. If it does, it will return the available submodules, if not an Attribute Error will be raised.
So for example, for a package like scipy:
import scipy
print(scipy.__all__) # prints all contents.