Quote:So the two lines are required if I don't want to bother prepending each method/property with "foo."
No. You don't need "import foo" to do "from foo import bar". If "bar" is the only thing you want to use from "foo", you only need to use "from foo import bar". When you see "import x" and "from x import y" it is usually because "y" is a submodule, and the program wants to use objects from both the "x" and "x.y" modules.
Sometimes a module contains submodules. This is often done in packages that contain a lot of features, but not all features of a package are used in every program. Breaking up the package into submodules not only makes the code more manageable, it also reduces import time.
Like any module, if you want to use objects in a submodule it needs to be imported. For example:
import tkinter as tk # imports tkinter module
from tkinter import ttk # imports the ttk submodule
root = tk.Tk()
lttk.Label(root, text="Hello").pack()
root.mainloop()
If I try to write like this:
import tkinter as tk
root = tk.Tk()
tk.ttk.Label(root, text="Hello").pack()
root.mainloop()
I get an error.
Error:
AttributeError: module 'tkinter' has no attribute 'ttk'
I get the error because ttk is a submodule of the tkinter package. It is not an attribute of the tkinter module.
I could also import the submodule like this:
import tkinter as tk # import tkinter
import tkinter.ttk as ttk # import tkinter.ttk
root = tk.Tk()
ttk.Label(root, text="Hello").pack()
root.mainloop()
You can read about modules and packages here:
https://docs.python.org/3/tutorial/modules.html