py_import (erlang_python v3.0.0)
View SourceImport and path registry for Python interpreters.
This module manages the global import and path registries that are applied to all Python interpreters. Imports and paths are applied immediately to all running interpreters and stored for new interpreters.
Configuration
Imports and paths can be configured in the application environment:
{erlang_python, [
{imports, [{json, dumps}, {math, sqrt}, {os, getcwd}]},
{paths, ["/path/to/modules"]}
]}Examples
%% Register modules for import in all interpreters (immediate + future)
ok = py_import:ensure_imported(json).
ok = py_import:ensure_imported(math, sqrt).
%% Add paths to sys.path in all interpreters (immediate + future)
ok = py_import:add_path("/path/to/my/modules").
%% Check registry contents
Imports = py_import:all_imports().
Paths = py_import:all_paths().
Summary
Functions
Add a path to sys.path in all interpreters.
Add multiple paths to sys.path in all interpreters.
Get all registered imports from the global registry.
Get all registered paths from the global registry.
Clear all registered imports from the global registry.
Clear all registered paths from the global registry.
Register a module for import in all interpreters.
Register a module/function for import in all interpreters.
List all registered imports.
Initialize the import and path registry ETS tables.
Check if a module is registered in the import registry.
Check if a module/function is registered in the import registry.
Check if a path is registered in the path registry.
Types
Functions
Add a path to sys.path in all interpreters.
Adds the path immediately to sys.path in all running interpreters (contexts and event loops) and stores it for future interpreters.
Example:
ok = py_import:add_path("/path/to/my/modules"),
{ok, Result} = py:call(mymodule, myfunc, []).
Add multiple paths to sys.path in all interpreters.
Adds all paths to the global path registry. Paths are added in order, so the first path in the list will be first in sys.path.
Example:
ok = py_import:add_paths(["/path/to/lib1", "/path/to/lib2"]),
{ok, Result} = py:call(mymodule, myfunc, []).
Get all registered imports from the global registry.
Returns a list of {Module, Func | all} tuples representing all modules/functions registered for automatic import.
Example:
ok = py_import:ensure_imported(json),
ok = py_import:ensure_imported(math, sqrt),
[{<<"json">>, all}, {<<"math">>, <<"sqrt">>}] = py_import:all_imports().
-spec all_paths() -> [binary()].
Get all registered paths from the global registry.
Returns a list of paths in the order they were added.
Example:
ok = py_import:add_path("/path/to/modules"),
[<<"/path/to/modules">>] = py_import:all_paths().
-spec clear_imports() -> ok.
Clear all registered imports from the global registry.
Removes all entries from the registry. Does not affect already-running interpreters.
-spec clear_paths() -> ok.
Clear all registered paths from the global registry.
Removes all entries from the path registry. Does not affect already-running interpreters.
Register a module for import in all interpreters.
Imports the module immediately in all running interpreters and adds it to the registry for future interpreters.
The __main__ module is never cached.
Example:
ok = py_import:ensure_imported(json),
{ok, Result} = py:call(json, dumps, [Data]).
Register a module/function for import in all interpreters.
Imports the module immediately in all running interpreters and adds it to the registry for future interpreters.
The __main__ module is never cached.
Example:
ok = py_import:ensure_imported(json, dumps),
{ok, Result} = py:call(json, dumps, [Data]).
List all registered imports.
Returns a map of modules to their registered functions. Module names are binary keys, function lists are the values. An empty list means only the module is registered (no specific functions).
Example:
ok = py_import:ensure_imported(json),
ok = py_import:ensure_imported(json, dumps),
ok = py_import:ensure_imported(json, loads),
ok = py_import:ensure_imported(math),
{ok, #{<<"json">> => [<<"dumps">>, <<"loads">>],
<<"math">> => []}} = py_import:import_list().
-spec init() -> ok.
Initialize the import and path registry ETS tables.
This is called automatically during application startup. Also loads imports and paths from application config. Safe to call multiple times - does nothing if already initialized.
Check if a module is registered in the import registry.
Check if a module/function is registered in the import registry.
Check if a path is registered in the path registry.