0

As far as I understood, from module import * means that everything from the module will be locally available.

In my code I found:

from tkinter import *

and

from tkinter import filedialog

Looking back, I figured I could drop this last line, but then it is unavailable:

NameError: name 'filedialog' is not defined.

What am I missing?

PascalVKooten
  • 20,643
  • 17
  • 103
  • 160
  • To keep it short, this is your answer: [filedialog is a **module** of tkinter][1] [1]: http://stackoverflow.com/questions/9239514/filedialog-tkinter-and-opening-files – angryfruitsalad Aug 14 '13 at 16:17
  • The `*` import does *not* import submodule. It simply imports the selected package and puts everything in the global namespace. There is no difference in "things loaded" between `import tkinter` and `from tkinter import *`, the difference is that the latter will pollute the namespace. If the `__init__.py` file does not import submodules then you *must* import them explicitly(and this seems the case). – Bakuriu Aug 14 '13 at 17:02

1 Answers1

1

From what I understand, Tkinter is a package (which means that it contains other modules). From Tkinter import * will only give you the default modules.

from the documentation:

6.4.1. Importing * From a Package

Now what happens when the user writes from sound.effects import *? Ideally, one would hope that this somehow goes out to the filesystem, finds which submodules are present in the package, and imports them all. This could take a long time and importing sub-modules might have unwanted side-effects that should only happen when the sub-module is explicitly imported.

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. It is up to the package author to keep this list up-to-date when a new version of the package is released. Package authors may also decide not to support it, if they don’t see a use for importing * from their package. For example, the file sounds/effects/init.py could contain the following code:

Please, read the following post for another answer to your question. filedialog, tkinter and opening files

Community
  • 1
  • 1
edi_allen
  • 1,878
  • 1
  • 11
  • 8