0

This import statement:

from tkinter import *

does not import tkinter.filedialog. Why it doesn't?

Bryan Oakley
  • 370,779
  • 53
  • 539
  • 685
mcu
  • 3,302
  • 8
  • 38
  • 64

2 Answers2

4

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__.py code 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.

Community
  • 1
  • 1
Anand S Kumar
  • 88,551
  • 18
  • 188
  • 176
  • 1
    This seems to contradict this highly rated [answer](http://stackoverflow.com/a/64130/2228746). _If the __all__ above is commented out, this code will then execute to completion, as the default behaviour of import * is to import all symbols that do not begin with an underscore, from the given namespace._ `filedialog` does not begin with an underscore. – mcu Oct 11 '15 at 13:03
  • They are talking about a single module, that is not true for packages (Like I emphasised, `tkinter` is a package, and it does not automatically import modules from within the package (if the `__all__` is not specified). As also very well explained in the documentation that I have linked in my answer. – Anand S Kumar Oct 11 '15 at 13:05
  • Packages are basically directories that can contain other packages/modules . Modules are single `.py` files. If you want to know more about packages, [this](https://docs.python.org/3/tutorial/modules.html#packages) would be a good starting place – Anand S Kumar Oct 11 '15 at 13:08
1

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.
Dimitris Fasarakis Hilliard
  • 150,925
  • 31
  • 268
  • 253