Why do I need to explicitly import the font module from the tkinter module even if have imported the full module using "*"? Why do I need to explicitly import the font module from the tkinter module even if have imported the full module using "*"? tkinter tkinter

Why do I need to explicitly import the font module from the tkinter module even if have imported the full module using "*"?


import * does not import everything. One way that it doesn't import everything is that it does not automatically search for submodules of a package. font is a submodule of the tkinter package, and if it has not already been loaded by some other import, from tkinter import * will not find tkinter.font.


The answer is simple: Python doesn't automagically import all module hierarchies, just because you import the top-level one. Those who do (e.g. os, which will make os.path available) have to explicitly write code for that .

Just add import tkinter.font, and it works

However, as IDLE uses tkinter itself, it has already imported tkinter.font, thus you think you can get away without that import , I hope this helps : )


Modules can have submodules and/or functions, variables, etc. What gets imported by from <module> import * depends on how the module was implemented. Most modules will not automatically import submodules.
In this case, tkinter is the main module, and font is a submodule, and tkinter was not designed to import all submodules automatically. So when you do from tkinter import *, you are grabbing all the functions and variables, but not the submodules. The submodules must be explicitly imported to be used. You can enter:

from tkinter import *from tkinter import font

Or you could also enter:

import tkinter  # actually not needed since the below "does both"import tkinter.font

The difference would be whether you want to use font.Font(... or tkinter.font.Font(...