This module contains the core functions required to create an import hook.

class ideas.import_hook.IdeasLoader(filename, callback_params=None, create_module=None, exec_=None, module_class=None, source_init=None, transform_ast=None, transform_bytecode=None, transform_source=None)[source]

A custom loader which will transform the source prior to its execution


Potential replacement for the default create_module method.


Import the source code, transform it before executing it so that it is known to Python.

class ideas.import_hook.IdeasMetaFinder(callback_params=None, create_module=None, excluded_paths=None, exec_=None, extensions=None, hook_name=None, module_class=None, source_init=None, transform_ast=None, transform_bytecode=None, transform_source=None)[source]

A custom finder to locate modules. The main reason for this code is to ensure that our custom loader, which does the code transformations, is used.

find_spec(fullname, path, target=None)[source]

finds the appropriate properties (spec) of a module, and sets its loader.

ideas.import_hook.create_hook(callback_params=None, create_module=None, console_dict=None, exec_=None, extensions=None, first=True, hook_name=None, ipython_ast_node_transformer=None, module_class=None, source_init=None, transform_ast=None, transform_bytecode=None, transform_source=None)[source]

Function to facilitate the creation of an import hook.

Each of the following parameter is optional; most of these are never needed except in some unusual import hooks.

Usually, at least one of transform_ast, transform_bytecode, and transform_source should be specified.

  • callback_params: a dict containing keyword parameters to be passed back to the transform_source function.

  • create_module: a custom function to create a module object instead of using Python’s default.

  • console_dict: a dict object used as ‘locals’ with the Ideas console, instead of its usual default.

  • exec_: a custom method used to execute the source code inside a module’s dict.

  • extensions: a list of file extensions, other than the usual .py, etc., used to identify modules containing source code.

  • first: if True, the custom hook will be used as the first location in sys.meta_path, to look for source files.

  • hook_name: used to give a more readable repr to the hook created.

  • ipython_ast_node_transformer: used to do AST transformations in an IPython/Jupyter environment. It should be a class derived from ast.NodeTransformer and return a node.

  • module_class: custom class to use for the module created instead of the default one assigned by Python.

  • source_init: custom code to be executed before any code from a user is executed. For example, if one creates an import hook that treats every float as a Decimal object, this custom code could be:

    from decimal import Decimal
  • transform_ast: used to do AST transformations in a Python environment (excluding IPython/Jupyter). It should be a class derived from ast.NodeTransformer, eventually returning a tree object.

  • transform_bytecode: used to mutate a code object.

  • transform_source: used to transform some source code prior to execution.


Takes an AST transformer designed to work with IPython, and wraps it to add a warning in case the user would like to see how the code is actually transformed, since this is not possible when using IPython.


Takes a source transform and makes returns an IPython compatible source transformer.


Function used to remove a previously imported hook inserted in sys.meta_path