Skip to main content

Convenience functions related to modules and importing.

Project description

Convenience functions related to modules and importing.

Latest release 20220606: New import_extra(extra_package_name,distinfo) function to politely try to import a package which is associated with an extra.

Function direct_imports(src_filename, module_name=None)

Crudely parse src_filename for import statements. Return the set of directly imported module names.

If module_name is not None, resolve relative imports against it. Otherwise, relative import names are returned unresolved.

This is a very simple minded source parse.

Function import_extra(extra_package_name, distinfo)

Try to import the package named extra_package_name using importlib.import_module and return the imported package.

If an ImportError is raised, riffle through the extras mapping in distinfo['extras_requires'] for the package name, and emit an informative warning about the extras which require this package and whose use a pip install time would bring the package in. The original ImportError is then reraised.

If no extra is found this is presumed to be an error by the caller and a RuntimeError is raised. This function is for internal use as:

pkg = import_extra('some_package', DISTINFO)

which passes in the source module's DISTINFO mapping, which I use as the basis for my package distributions.

A fuller example from my cs.timeseries module's plot command line mode:

def cmd_plot(self, argv):
  """ Usage: {cmd} datadir days fields...
  """
  try:
    import_extra('plotly', DISTINFO)
  except ImportError as e:
    raise GetoptError(
        "the plotly package is not installed: %s" % (e,)
    ) from e

which produces this output:

timeseries.py: plot: import_extra('plotly'): package not available; the following extras pull it in: ['plotting']
timeseries.py: the plotly package is not installed: timeseries.py: plot: import_extra('plotly'): No module named 'plotly'

Function import_module_from_file(module_name, source_file, sys_path=None)

Import a specific file as a module instance, return the module instance.

Parameters:

  • module_name: the name to assign to the module
  • source_file: the source file to load
  • sys_path: optional list of paths to set as sys.path for the duration of this import; the default is the current value of sys.path

Note that this is a "bare" import; the module instance is not inserted into sys.modules.

Warning: sys.path is modified for the duration of this function, which may affect multithreaded applications.

Function import_module_name(module_name, name, path=None, lock=None)

Import module_name and return the value of name within it.

Parameters:

  • module_name: the module name to import.
  • name: the name within the module whose value is returned; if name is None, return the module itself.
  • path: an array of paths to use as sys.path during the import.
  • lock: a lock to hold during the import (recommended).

Function module_attributes(M)

Generator yielding the names and values of attributes from a module which were defined in the module.

Function module_files(M)

Generator yielding .py pathnames involved in a module.

Function module_names(M)

Return a list of the names of attributes from a module which were defined in the module.

Release Log

Release 20220606: New import_extra(extra_package_name,distinfo) function to politely try to import a package which is associated with an extra.

Release 20210123: module_attributes: skip values from other modules if we know the module (computed values like tuples have no module and still need to be returned).

Release 20200521:

  • New import_module_from_file function to import a Python file as a module instance.
  • New direct_imports(src_filename,module_name=None) returning the set of directly imports module names.

Release 20190101: New functions: module_names, module_attributes.

Release 20160918:

  • New generator function module_files yielding pathnames.
  • import_module_name: accept name=None, just return the module.
  • Add empty "install_requires" for DISTINFO completeness.

Release 20150116: Initial PyPI release.

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

cs.py.modules-20220606.tar.gz (5.2 kB view details)

Uploaded Source

Built Distribution

cs.py.modules-20220606-py3-none-any.whl (5.7 kB view details)

Uploaded Python 3

File details

Details for the file cs.py.modules-20220606.tar.gz.

File metadata

  • Download URL: cs.py.modules-20220606.tar.gz
  • Upload date:
  • Size: 5.2 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.8.0 pkginfo/1.8.1 readme-renderer/30.0 requests/2.27.1 requests-toolbelt/0.9.1 urllib3/1.26.7 tqdm/4.62.3 importlib-metadata/4.8.2 keyring/23.3.0 rfc3986/1.5.0 colorama/0.4.4 CPython/3.9.13

File hashes

Hashes for cs.py.modules-20220606.tar.gz
Algorithm Hash digest
SHA256 38775c631feaf8116cf972ada719327129e71d5fa9708a0941542074e1b488b3
MD5 3430e6591592e65d4597057856e108eb
BLAKE2b-256 d672cff8944498cc3cea68e1cf915358c3b6d6b93b31c17e4b3d72647a50b0ae

See more details on using hashes here.

File details

Details for the file cs.py.modules-20220606-py3-none-any.whl.

File metadata

  • Download URL: cs.py.modules-20220606-py3-none-any.whl
  • Upload date:
  • Size: 5.7 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.8.0 pkginfo/1.8.1 readme-renderer/30.0 requests/2.27.1 requests-toolbelt/0.9.1 urllib3/1.26.7 tqdm/4.62.3 importlib-metadata/4.8.2 keyring/23.3.0 rfc3986/1.5.0 colorama/0.4.4 CPython/3.9.13

File hashes

Hashes for cs.py.modules-20220606-py3-none-any.whl
Algorithm Hash digest
SHA256 3f7865526adb605bbfd6d8575e7443a99a4939799d9dce2f22c04612ac0984f3
MD5 4f7f507489498ca4a20deb67ea1cb08f
BLAKE2b-256 b3b71afa6bf3d0dda8f85bad7e844b9bcc6c09f57ddd330198e1c7d68cdf04bd

See more details on using hashes here.

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page