Python Forum
Couple of questions about modules
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Couple of questions about modules
#1
Hello,

A couple of newbie questions about modules:

1. What does this do? Why import the same module twice?
import gpxpy
import gpxpy.gpx
2. How can I find the list of methods and properties a module provides?

Is help(mymodule) and dir(mymodule) the only ways?

Thank you.
Reply
#2
check our Modules tutorials
Part 1
Part 2
Part 3
If you can't explain it to a six year old, you don't understand it yourself, Albert Einstein
How to Ask Questions The Smart Way: link and another link
Create MCV example
Debug small programs

Reply
#3
Apparently, gpxpy.gpx is a subpackage of gpxpy. Subpackages are not necessarily available in the package's namespace and you may need to import them separately (this allows to import only what you really need).

Here is a script list_subpackages.py to list recursively the subpackages of a package
#!/usr/bin/env python3
# list_subpackages.py
# Author: Gribouillis for www.python-forum.io, idea borrowed from Federico Tomassetti's blog
"""usage: use_subpackages.py <python module>

Version 0.2.1 bugfix
Version 0.2.0 bugfix and shorter code
Version 0.1.0 handles the case when a packages changes its __path__ when
it is imported.

TODO: argument parsing

"""
import pkgutil
import sys
import importlib.util

__version__ = "0.2.1"

try:
    ModuleNotFoundError
except NameError: # python < 3.6
    ModuleNotFoundError = AttributeError

def explore_package(module_name):
    try:
        spec = importlib.util.find_spec(module_name)
    except ModuleNotFoundError:
        return
    if spec is None:
        return
    locations = spec.submodule_search_locations
    if locations is None:
        return
    for _, qname, _ in pkgutil.walk_packages(locations, module_name+'.'):
        yield qname

if __name__ == '__main__':
    for name in explore_package(sys.argv[1]):
        print(name)
For example with module urllib
Output:
λ ./list_subpackages.py urllib urllib.error urllib.parse urllib.request urllib.response urllib.robotparser
Note that submodules may exist in a module that is not a package. An example is os.path. You can import os and use os.path.expanduser() for example without importing explicitly os.path.

You can use vars(module) to see the contents of an imported module as a dictionary.
Reply
#4
(Aug-24-2018, 07:27 AM)Winfried Wrote: 1. What does this do? Why import the same module twice?
Because that's the way they have made the package link gpxpy.
Could it just have been import gpxpy yes.
Then they have to modify __init__.py
You see that now only from . import parser as mod_parser is added there.
This mean that only parse get used bye using import gpxpy.
Using pdir2 for easier show what going on.
>>> import gpxpy
>>> import pdir

>>> pdir(gpxpy)
module attribute:
    __cached__, __file__, __loader__, __name__, __package__, __path__, __spec__
property:
    __builtins__
special attribute:
    __doc__
function:
    parse: Parse xml (string) or file object. This is just an wrapper for

>>> gpxpy.parse
<function parse at 0x04458660>
Doing import gpxpy.gpx,will add the rest geo, gpx, gpxfield, utils.
>>> pdir(gpxpy)
module attribute:
    __cached__, __file__, __loader__, __name__, __package__, __path__, __spec__
property:
    __builtins__, geo, gpx, gpxfield, utils
special attribute:
    __doc__
function:
    parse: Parse xml (string) or file object. This is just an wrapper for

# Now rest of import work.
>>> gpxpy.geo
<module 'gpxpy.geo' from '......geo.py'>
It's kind of okay to have 2 import,because they have in in documentation.
It could be cleaner to have one import,they can have there reason(good or bad) to have parser() function in own import.
Reply
#5
Thanks.

Even after reading the three parts, I'm still mostly in the dark (module, submodule, package, subpackage, library, namespace, etc.), but I'll read up and experiment.
Reply
#6
(Aug-24-2018, 07:56 PM)Winfried Wrote: I'm still mostly in the dark (module, submodule, package, subpackage, library, namespace, etc.), but I'll read up and experiment.
All this is confusing when new to Python,i have a tutorial here.
It cover a lot stuff,so i think of maybe make a more basic tutorial like module vs package.
Reply
#7
Thank you.
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  A couple of questions Led_Zeppelin 1 892 Sep-30-2022, 07:05 PM
Last Post: deanhystad
  More than a couple problems. MurphysLaw 7 3,190 Apr-12-2021, 09:11 PM
Last Post: virginiaturner
  Need help understanding a couple of functions (encrypt,decrypt, int_to_bytes) xoani 0 1,967 Jun-09-2019, 03:25 PM
Last Post: xoani
  just a couple questions.... medievil 5 3,560 May-05-2018, 03:13 AM
Last Post: medievil
  Discord bot that asks questions and based on response answers or asks more questions absinthium 1 34,439 Nov-25-2017, 06:21 AM
Last Post: heiner55
  A couple of questions about pip install Athenaeum 3 3,542 Oct-13-2017, 08:18 PM
Last Post: nilamo
  Modules issue, pip3 download modules only to pyhton3.5.2 not the latest 3.6.1 bmohanraj91 6 8,352 May-25-2017, 08:15 AM
Last Post: wavic

Forum Jump:

User Panel Messages

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