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
  ImportError: cannot import name 'NullHandler' from 'logging' sukanta80 0 603 Jul-28-2025, 03:26 PM
Last Post: sukanta80
  Dynamic Module Import Error DaddyMAN 3 1,542 Jun-20-2025, 12:07 AM
Last Post: Pedroski55
  ImportError: cannot import name 'Pyfhel' from 'Pyfhel' Anldra12 11 7,926 Jun-16-2025, 07:08 AM
Last Post: ItsTheGoose
  Error on import: SyntaxError: source code string cannot contain null bytes kirkwilliams2049 10 26,587 May-26-2025, 01:55 PM
Last Post: deanhystad
Question Import vs from x import Calab 1 756 May-15-2025, 04:09 PM
Last Post: snippsat
  I'm trying to import a dataset into a Jupyter Notebook Hisilat03 0 1,193 Mar-24-2025, 09:26 AM
Last Post: Hisilat03
  beginner doesn't understand import cimerio 3 1,121 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 1,305 Dec-13-2024, 06:50 AM
Last Post: Gribouillis
  python 3.13 : import whois HansieB 1 1,348 Nov-30-2024, 02:58 PM
Last Post: snippsat
  import data (.csv) into Jupyter notebook oranstprotonme 2 2,096 Aug-14-2024, 07:08 PM
Last Post: oranstprotonme

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020
This forum uses Lukasz Tkacz MyBB addons.
Forum use Krzysztof "Supryk" Supryczynski addons.