tkrajina / gpxpy

gpx-py is a python GPX parser. GPX (GPS eXchange Format) is an XML based file format for GPS tracks.
Apache License 2.0
1.01k stars 223 forks source link

[newbie] Why is a module imported twice? #228

Open Shohreh opened 3 years ago

Shohreh commented 3 years ago

Hello,

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

import gpxpy
import gpxpy.gpx

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

Thank you.

MinchinWeb commented 3 years ago

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!