Python Forum
[newbie] Why is a module imported twice?
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
[newbie] Why is a module imported twice?
#1
Hello,

In this example, I notice the module is imported twice.

import gpxpy
import gpxpy.gpx
I assume it's a way to save some typing by pointing directly to some sub-module. Is that right?

Thank you.
Reply
#2
I think gpxpy is the module of code and .gpx is a file type
Reply
#3
Fidgety - no.

See the documentation at Python docs Look particularly at part 5.2 and onward.

There are a variety of reasons for importing submodules separately, and while yes it can help shorten typing (and perhaps a bit of execution time) esp if you use something like import matplotlib.pyplot as plt

a bigger difference is that the import of each submodule executes that submodule's __init__.py
Reply
#4
I don't know that I agree with this:
Quote:a bigger difference is that the import of each submodule executes that submodule's __init__.py
This would be true if each submodule was in a subdirectory of the package, but that doesn't have to be true for submodules.

Take tkinter for example. All the submodules are .py files in the PythonXX\Lib\tkinter folder. import tkinter.ttk imports the ttk module. It would also execute the __init__.py in that folder. However, most programs import tkinter and then tkinter.ttk, so the __init__.py file is already executed by the time we get around to importing ttk. So what you say could be true, but it could also be false.

I was surprised to find out I can do this:
import tkinter.ttk

x = tkinter.Tk()
tkinter.Label(x, text="This is a long label").pack()
tkinter.Label(x, text="This is a long label").pack()
tkinter.Label(x, text="This is a long label").pack()
x.mainloop()
In this example the import tkinter.ttk does execute tkinter.__init__.py, and all the tkinter symbols are added to the namespace.
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Error when refering to class defined in 'main' in an imported module HeRo 2 579 Apr-13-2021, 07:22 PM
Last Post: HeRo
Star NameError – function doesn't recognize imported modules Sir 4 983 Dec-01-2020, 06:36 AM
Last Post: Sir
  how to do setattr() from an imported module nutron 3 1,404 Sep-20-2019, 08:16 PM
Last Post: nutron
  argparse and imported modules spatialdawn 2 3,032 Dec-01-2018, 12:35 PM
Last Post: spatialdawn
  global namespace of an imported function (2 Qs) Skaperen 4 1,553 Oct-09-2018, 12:30 AM
Last Post: Skaperen
  passing a value to imported code Skaperen 0 954 Sep-28-2018, 03:59 AM
Last Post: Skaperen
  function NOT imported from a module Skaperen 10 2,909 Aug-31-2018, 07:30 AM
Last Post: Gribouillis
  [SYS + OS] Script not Imported, Error Found ! JamieVanCadsand 2 2,219 Sep-22-2017, 08:03 PM
Last Post: snippsat
  decorate an imported function? Skaperen 3 5,546 May-22-2017, 08:05 PM
Last Post: nilamo

Forum Jump:

User Panel Messages

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