Bottom Page

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.
Quote
#2
check our Modules tutorials
Part 1
Part 2
Part 3
Quote
#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.
snippsat likes this post
Quote
#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.
Quote
#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.
Quote
#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.
Quote
#7
Thank you.
Quote

Top Page

Possibly Related Threads...
Thread Author Replies Views Last Post
  Need help understanding a couple of functions (encrypt,decrypt, int_to_bytes) xoani 0 276 Jun-09-2019, 03:25 PM
Last Post: xoani
  just a couple questions.... medievil 5 1,061 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 3,032 Nov-25-2017, 06:21 AM
Last Post: heiner55
  A couple of questions about pip install Athenaeum 3 1,300 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 4,371 May-25-2017, 08:15 AM
Last Post: wavic

Forum Jump:


Users browsing this thread: 1 Guest(s)