3

I'm new to python and I have some confusion in the import methods.

I thought that if I do:

from tkinter import *

I should be able to use:

filedialog.askopenfilename

However, it gives an error saying unsolved references.

If I do:

from tkinter import filedialog
filedialog.askopenfilename

It works correctly and I get why it does it. However, I don't understand why Python will not let me do filedialog.askopenfilename with from tkinter import *

Tomerikoo
  • 18,379
  • 16
  • 47
  • 61
attat
  • 57
  • 9

1 Answers1

3

Read this and this to get some more insight on how from <foo> import * works.

There is two different kinds of from targets: modules and packages.

  • A module is a single python file. If you import * from a module, it imports everything, except private names starting with '_'.
  • A package is a folder, like tkinter. If you import * from a package, it imports all the names that exist in its __init__.py. If __init__.py does not exist, it imports nothing.

In both cases, the behaviour of import * can be overridden by defining __all__.

tkinter is a folder(=package) and does not define __all__ in its __init__.py, so from tkinter import * only imports the names in its __init__.py, like Tk, Button or Label, but not filedialog.

Finomnis
  • 18,094
  • 1
  • 20
  • 27
  • I have a follow up question. From the python document it says if _ _all_ _ is not defined it will not import submodules but "imports whatever names are defined in the package". Does it mean like "def something()" that is not private inside the _ _init_ _.py will be imported? Also does 'class hi: def hello()' counts as a submodule inside _ _ init _ _.py? – attat Jul 23 '19 at 15:32
  • The details and exceptions can be read [here](https://docs.python.org/3/tutorial/modules.html#importing-from-a-package). Yes, I think everything that is defined inside the `__init__.py` will be imported. No, a class is not a submodule. You cannot import functions of classes, only the entire class at once. – Finomnis Jul 23 '19 at 15:40
  • oh thx. and this is continuation of my question. If I do `from tkinter import *` I was able to use Tk(), Label(),Button() etc. modules without adding anything infront of it. However, I could not find them declared inside the `__init__.py`. So in this case how is it working? – attat Jul 23 '19 at 15:42
  • Not sure what you are talking about? They are all declared in `__init__.py`: https://github.com/python/cpython/blob/3.7/Lib/tkinter/__init__.py – Finomnis Jul 23 '19 at 15:56
  • oh I thought the modules where written as `button.py` in the script. The `class button(Widget)` was the modules that were declared. And one very last question. How do I identify submodules in `__init.py__`? – attat Jul 23 '19 at 16:10
  • I'm not sure what you mean with 'identify submodules' :/ – Finomnis Jul 23 '19 at 16:14
  • `from tkinter import *; a = Tk()` works, so can you clarify what you mean by "if you import * from a package (like tkinter), it imports nothing" – SyntaxVoid Jul 23 '19 at 16:25
  • I'll correct, it imports only the definitions of `__init__.py`. I adjust my answer. – Finomnis Jul 23 '19 at 16:26
  • oh the submodules are created using folders and not inside a single py script. That was were I was lost. So if I create a new folder(is this subpackage?) inside a main folder(package) and create a .py script, then that will be a submodule. Did I understand it correctly? – attat Jul 23 '19 at 16:35
  • If you create a folder, you create a *package*. The package then can contain several *modules* (python files), or more *sub-packages* (folders). `import` does not directly show whether you are importing a package or module, the command is the same for both. – Finomnis Jul 23 '19 at 16:36