pub async fn pre_initialize(
python_stdlib: &Path,
site_packages: Option<&Path>,
imports: &[&str],
extensions: &[NativeExtension],
setup_code: Option<&str>,
) -> Result<Vec<u8>>Available on crate feature
preinit only.Expand description
Pre-initialize a Python component with native extensions.
This function links the component with native extensions, runs the Python interpreter’s initialization, optionally imports modules, and captures the initialized memory state into the returned component.
§Arguments
python_stdlib- Path to Python standard library directorysite_packages- Optional path to site-packages directoryimports- Modules to import during pre-init (e.g., [“numpy”, “pandas”])extensions- Native extensions to link into the componentsetup_code- Optional Python code to execute after imports, baked into the snapshot. Use this to pre-create objects (e.g., a Jinja2SandboxedEnvironment) so every sandbox starts with them in COW memory.
§Returns
The pre-initialized component bytes, ready for instantiation.
§Errors
Returns an error if pre-initialization fails (e.g., Python init error, import failure, or setup code exception).