_modules module

exception pyflyby._modules.ErrorDuringImportError

Exception raised by import_module if the module exists but an exception occurred while attempting to import it. That nested exception could be ImportError, e.g. if a module tries to import another module that doesn’t exist.

class pyflyby._modules.ModuleHandle(arg)

A handle to a module.

_cls_cache: Dict[Any, Any] = {}
classmethod _from_filename(filename)
classmethod _from_module(module)
classmethod _from_modulename(modulename)
static _member_from_node(node)
property ancestors
property block
classmethod containing(identifier)

Try to find the module that defines a name such as a.b.c by trying to import a, a.b, and a.b.c.

Returns:

The name of the ‘deepest’ module (most commonly it would be a.b in this example).

Return type:

Module

property exists

Return whether the module exists, according to pkgutil. Note that this doesn’t work for things that are only known by using sys.meta_path.

property exports

Get symbols exported by this module.

Note that this will not recognize symbols that are dynamically introduced to the module’s namespace or __all__ list.

Return type:

ImportSet or None

Returns:

Exports, or None if nothing exported.

property filename

Return the filename, if appropriate.

The module itself will not be imported, but if the module is not a top-level module/package, accessing this attribute may cause the parent package to be imported.

Return type:

Filename

static list()

Enumerate all top-level packages/modules.

The current working directory is excluded for autoimporting; if we autoimported random python scripts in the current directory, we could accidentally execute code with side effects.

Also exclude any module names that are not legal python module names (e.g. “try.py” or “123.py”).

Return type:

list[str]

Returns:

A list of all importable module names

property module

Return the module instance.

Return type:

types.ModuleType

Raises:
  • ErrorDuringImportError – The module should exist but an error occurred while attempting to import it.

  • ImportError – The module doesn’t exist.

name: DottedIdentifier
property parent
property submodules

Enumerate the importable submodules of this module.

>>> ModuleHandle("email").submodules
(..., ModuleHandle('email.encoders'), ..., ModuleHandle('email.mime'), ...)
Return type:

tuple of ModuleHandle s

property text
pyflyby._modules._cached_module_finder(importer, prefix='')

Yield the modules found by the importer.

The importer path’s mtime is recorded; if the path and mtime have a corresponding cache file, the modules recorded in the cache file are returned. Otherwise, the cache is rebuilt.

Parameters:
  • importer (importlib.machinery.FileFinder) – FileFinder importer that points to a path under which imports can be found

  • prefix (str) – String to affix to the beginning of each module name

Returns:

Tuples containing (prefix+module name, a bool indicating whether the module is a package or not)

Return type:

Generator[tuple[str, bool], None, None]

pyflyby._modules._fast_iter_modules()

Return an iterator over all importable python modules.

This function patches pkgutil.iter_importer_modules for importlib.machinery.FileFinder types, causing pkgutil.iter_importer_modules to call our own custom _iter_file_finder_modules instead of pkgutil._iter_file_finder_modules.

Return type:

Generator[ModuleInfo, None, None]

Returns:

The modules that are importable by python

pyflyby._modules._format_path(path)

Format a path for printing as a log message.

If the path is a child of $HOME, the prefix is replaced with “~” for brevity. Otherwise the original path is returned.

Parameters:

path (Union[str, pathlib.Path]) – Path to format

Returns:

Formatted output path

Return type:

str

pyflyby._modules._my_iter_modules(path, prefix='')
pyflyby._modules._remove_import_cache_dir(path)

Remove an import cache directory.

Import cache directories exist in <user cache dir>/pyflyby/, and they should contain just a single file which itself contains a JSON blob of cached import names. We therefore only delete the requested path if it is a directory.

Parameters:

path (pathlib.Path) – Import cache directory path to remove

pyflyby._modules.import_module(module_name)
pyflyby._modules.pyc_to_py(filename)