tkrajina/gpxpy

[newbie] Why is a module imported twice?

Shohreh opened this issue · 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.

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!