Python Forum
[SOLVED] Why "import foo" + "from foo import bar"?
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
[SOLVED] Why "import foo" + "from foo import bar"?
#1
Question 
Hello,

Googling didn't help.

I often see examples that look like this:

import foo
from foo import bar
Why the second line?

Thank you.
Reply
#2
Module foo may contain many symbols, such as bar, baz, qux. After import foo, you can do
import foo
print(foo.bar, foo.baz, foo.qux)
After from foo import bar, you can do
from foo import bar
print(bar)
« We can solve any problem by introducing an extra level of indirection »
Reply
#3
So the two lines are required if I don't want to bother prepending each method/property with "foo."

Thank you.
Reply
#4
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
BashBedlam likes this post
Reply
#5
Thanks for the infos.
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  I'm trying to import a dataset into a Jupyter Notebook Hisilat03 0 713 Mar-24-2025, 09:26 AM
Last Post: Hisilat03
  beginner doesn't understand import cimerio 3 486 Feb-12-2025, 05:02 PM
Last Post: cimerio
  Python: How to import data from txt, instead of running the data from the code? Melcu54 1 615 Dec-13-2024, 06:50 AM
Last Post: Gribouillis
  python 3.13 : import whois HansieB 1 625 Nov-30-2024, 02:58 PM
Last Post: snippsat
  import data (.csv) into Jupyter notebook oranstprotonme 2 1,186 Aug-14-2024, 07:08 PM
Last Post: oranstprotonme
  'import requests' problem Curbie 2 782 Aug-14-2024, 01:04 AM
Last Post: Curbie
  import a function from another file using relative path paul18fr 6 2,842 Aug-01-2024, 06:40 AM
Last Post: paul18fr
Bug Python 3.12 cannot import local python files as modules sunflowerdog 25 18,229 Jul-31-2024, 01:52 PM
Last Post: sunflowerdog
  ImportError: cannot import name 'scipy_namespace_for' from 'scipy._lib._array_api' AgileAVS 0 1,750 Jul-15-2024, 08:45 AM
Last Post: AgileAVS
  Puzzling import issue that I have no idea how to solvr starseeker 3 1,506 Feb-21-2024, 05:07 PM
Last Post: deanhystad

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020