Convenience functions related to modules, packages and importing.
Latest release 20260531: direct_imports: new include_indented=False to include indented import statements.
Short summary:
direct_imports: Crudely parsesrc_filenameforimportstatements. Return the set of directly imported module names.import_extra: Try to import the package namedextra_package_nameusingimportlib.import_module.import_module_from_file: Import a specific file as a module instance, return the module instance.import_module_name: Importmodule_nameand return the value ofnamewithin it.module_attributes: Generator yielding the names and values of attributes from a module which were defined in the module.module_files: Generator yielding.pypathnames involved in a module.module_names: Return a list of the names of attributes from a module which were defined in the module.
Module contents:
-
direct_imports(src_filename, module_name, *, include_indented=False): Crudely parsesrc_filenameforimportstatements. Return the set of directly imported module names.Resolve relative imports against
module_name.This is a very simple minded source parse which understands nearly nothing about Python.
-
import_extra(extra_package_name, distinfo): Try to import the package namedextra_package_nameusingimportlib.import_module. Return the imported package.If an
ImportErroris 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 a
RuntimeErroris raised. This function is for internal use as:pkg = import_extra('some_package', DISTINFO)which passes in the source module's
DISTINFOmapping, which I use as the basis for my package distributions.A fuller example from my
cs.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' -
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 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.path
Note that this is a "bare" import; the module instance is not inserted into
sys.modules.Warning:
sys.pathis modified for the duration of this function, which may affect concurrent applications. -
import_module_name(module_name, name=None, sys_path=None, lock=None): Importmodule_nameand return the value ofnamewithin it.Parameters:
module_name: the module name to import.name: optional name within the module whose value is returned; ifnameisNone, return the module itself.sys_pathoptional list of paths to use assys.pathduring the import.lock: an optional lock to hold during the import (recommended).
Warning:
sys.pathis modified for the duration of this function, which may affect concurrent applications. -
module_attributes(M): Generator yielding the names and values of attributes from a module which were defined in the module. -
module_files(M): Generator yielding.pypathnames involved in a module. -
module_names(M): Return a list of the names of attributes from a module which were defined in the module.
Release Log
Release 20260531: direct_imports: new include_indented=False to include indented import statements.
Release 20250724:
- Improve resolution of relative imports. (Fix seems to be an overly strong word here, unfortunately.)
- Drop pretence at Python 2 support.
Release 20241122:
import_module_name: rename the path parameter to sys_path to match import_module_from_file, default sys_path to sys.path.
Release 20240630: direct_imports: fix off-by-one resolving leading dot relative import names.
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.
Release files for cs-py-modules 20260531
For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.
Source distribution (sdist)
| File | Size | Uploaded | |
|---|---|---|---|
| cs_py_modules-20260531.tar.gz | 5.0 kB | Details |
Built distribution (wheel)
| File | Interpreter | ABI | Platform | Reset |
|---|---|---|---|---|
| cs_py_modules-20260531-py2.py3-none-any.whl | Python 2, Python 3 | none | any | Details |
Total release size: 11.0 kB
Release files / cs_py_modules-20260531.tar.gz
| Download URL | cs_py_modules-20260531.tar.gz |
|---|---|
| Size | 5.0 kB |
| Tags | Source |
|
SHA-256 checksum How to use checksums |
934c42506939a9470904432b15d64dc8ae44b290b418640663beeffd3de105f2
|
|
BLAKE2b-256 checksum How to use checksums |
7a2981dfc181abd715bba046244aba747ee5f42686ef048c474c9c500fb5fb8c
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/6.1.0 CPython/3.13.1
|
Release files / cs_py_modules-20260531-py2.py3-none-any.whl
| Download URL | cs_py_modules-20260531-py2.py3-none-any.whl |
|---|---|
| Size | 6.1 kB |
| Tags | Python 2 Python 3 |
|
SHA-256 checksum How to use checksums |
18f404c92520a9f3c6e8fa2df54e68efe66c6f4b8366e94bbbb9bfbf2454c7fe
|
|
BLAKE2b-256 checksum How to use checksums |
39a34018efceac17cfaf65bc4c2ecaa8e65ad88481862cd7978a1b7652d83997
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/6.1.0 CPython/3.13.1
|