Couple of questions about modules - Printable Version +- Python Forum (https://python-forum.io) +-- Forum: Python Coding (https://python-forum.io/forum-7.html) +--- Forum: General Coding Help (https://python-forum.io/forum-8.html) +--- Thread: Couple of questions about modules (/thread-12425.html) |
Couple of questions about modules - Winfried - Aug-24-2018 Hello, A couple of newbie questions about modules: 1. What does this do? Why import the same module twice? import gpxpy import gpxpy.gpx2. How can I find the list of methods and properties a module provides? Is help(mymodule) and dir(mymodule) the only ways? Thank you. RE: Couple of questions about modules - buran - Aug-24-2018 check our Modules tutorials Part 1 Part 2 Part 3 RE: Couple of questions about modules - Gribouillis - Aug-24-2018 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 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.
RE: Couple of questions about modules - snippsat - Aug-24-2018 (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.
RE: Couple of questions about modules - Winfried - Aug-24-2018 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. RE: Couple of questions about modules - snippsat - Aug-24-2018 (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. RE: Couple of questions about modules - Winfried - Aug-24-2018 Thank you. |