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
  can not import anaconda pandas module. PySpark pandas module is imported!! aupres 0 680 Aug-06-2023, 01:09 AM
Last Post: aupres
  Can a module tell where it is being imported from? stevendaprano 3 1,140 Apr-12-2022, 12:46 AM
Last Post: stevendaprano
  module detecting if imported vs not Skaperen 1 1,637 Nov-19-2021, 07:43 AM
Last Post: Yoriz
  Error when refering to class defined in 'main' in an imported module HeRo 2 2,331 Apr-13-2021, 07:22 PM
Last Post: HeRo
  how to do setattr() from an imported module nutron 3 3,292 Sep-20-2019, 08:16 PM
Last Post: nutron
  argparse and imported modules spatialdawn 2 5,387 Dec-01-2018, 12:35 PM
Last Post: spatialdawn
  passing a value to imported code Skaperen 0 1,960 Sep-28-2018, 03:59 AM
Last Post: Skaperen
  function NOT imported from a module Skaperen 10 5,956 Aug-31-2018, 07:30 AM
Last Post: Gribouillis
  decorate an imported function? Skaperen 3 9,823 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