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?

2
  • To keep it short, this is your answer: [filedialog is a module of tkinter][1] [1]: stackoverflow.com/questions/9239514/… Commented Aug 14, 2013 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). Commented Aug 14, 2013 at 17:02

1 Answer 1

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

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.