Package: cs.py.modules
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.Functiondirect_imports(src_filename, module_name=None)Crudely parsesrc_filenameforimportstatements.
Return the set of directly imported module names.Ifmodule_nameis notNone,
resolve relative imports against it.
Otherwise, relative import names are returned unresolved.This is a very simple minded source parse.Functionimport_extra(extra_package_name, distinfo)Try to import the package namedextra_package_nameusingimportlib.import_moduleand return the imported package.If anImportErroris raised,
riffle through the extras mapping indistinfo['extras_requires']for the package name, and emit an informative warning
about the extras which require this package
and whose use apip installtime would bring the package in.
The originalImportErroris then reraised.If no extra is found this is presumed to be an error by the caller
and aRuntimeErroris raised.
This function is for internal use as:pkg = import_extra('some_package', DISTINFO)which passes in the source module'sDISTINFOmapping,
which I use as the basis for my package distributions.A fuller example from mycs.timeseriesmodule'splotcommand 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 ewhich 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'Functionimport_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 modulesource_file: the source file to loadsys_path: optional list of paths to set assys.pathfor the duration of this import;
the default is the current value ofsys.pathNote that this is a "bare" import;
the module instance is not inserted intosys.modules.Warning:sys.pathis modified for the duration of this function,
which may affect multithreaded applications.Functionimport_module_name(module_name, name, path=None, lock=None)Importmodule_nameand return the value ofnamewithin it.Parameters:module_name: the module name to import.name: the name within the module whose value is returned;
ifnameisNone, 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).Functionmodule_attributes(M)Generator yielding the names and values of attributes from a module
which were defined in the module.Functionmodule_files(M)Generator yielding.pypathnames involved in a module.Functionmodule_names(M)Return a list of the names of attributes from a module which were
defined in the module.Release LogRelease 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 modulesif 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.