gpxpy icon indicating copy to clipboard operation
gpxpy copied to clipboard

[newbie] Why is a module imported twice?

Open Shohreh opened this issue 3 years ago • 1 comments

Hello,

It's a newbie question: Why is the module imported twice?

import gpxpy
import gpxpy.gpx

https://pypi.org/project/gpxpy/

Thank you.

Shohreh avatar Apr 01 '21 17:04 Shohreh

Hi @Shohreh!

This has to do with the way Python importing works. When you import a module (such as gpxpy), it does not provide access to submodules (such as gpxpy.gpx) unless they are explicitly imported by the main module (which they aren't here).

One way to see this is to start the Python REPL at the command line (if you run just python, you'll probably be there), and try the following:

> import gpxpy
> dir(gpxpy)

The dir command will list everything in the supplied namespace. You'll see lots of things listed, but not gpx (the submodule).

Perhaps part of the confusion is that many library authors will import the submodules into the main module (or at least the ones meant to be public) as a convince, but as shown here, it isn't a requirement.

Hope that helps!

MinchinWeb avatar Apr 27 '21 00:04 MinchinWeb