Convenience functions related to modules and importing.
Project description
Convenience functions related to modules and importing.
Latest release 20241122:
import_module_name: rename the path parameter to sys_path to match import_module_from_file, default sys_path to sys.path.
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.
import_extra(extra_package_name, distinfo)
Try to import the package named extra_package_name
using importlib.import_module. 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'
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.path is modified for the duration of this function,
which may affect multithreaded applications.
import_module_name(module_name, name=None, sys_path=None, lock=None)
Import module_name and return the value of name within 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 array of paths to use assys.pathduring the import.lock: an optional lock to hold during the import (recommended).
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 .py pathnames 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 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.
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file cs_py_modules-20241122.tar.gz.
File metadata
- Download URL: cs_py_modules-20241122.tar.gz
- Upload date:
- Size: 5.4 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/5.1.1 CPython/3.10.6
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
b8cd3fc7fab75fb00aa393daf6761bc1aafae747d03c60125471223d89b980cf
|
|
| MD5 |
f0336648087ea0c921bc7683d864b728
|
|
| BLAKE2b-256 |
ee1aa67bd8ee7932f4877db857ad06637f738144fb7e721f9125393d38579086
|
File details
Details for the file cs_py_modules-20241122-py3-none-any.whl.
File metadata
- Download URL: cs_py_modules-20241122-py3-none-any.whl
- Upload date:
- Size: 5.9 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/5.1.1 CPython/3.10.6
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
aead7776722c7164224d560d458a55dab636a1ebbcf948f0d8b3ad0146e60baa
|
|
| MD5 |
3eb4916c64e48ae4c145ec266b617ae5
|
|
| BLAKE2b-256 |
fbf6bd4142d73b27599bd1bf5c0ca6ddee5f6b72ffc2020ea6341c2392ae12aa
|