Skip to main content

import hook to load rust projects built with maturin

Project description

Maturin Import Hook

A python import hook to automatically rebuild maturin projects and import stand-alone rust files.

Using this import hook reduces friction when developing mixed python/rust codebases because changes made to rust components take effect automatically like changes to python components do.

The import hook also provides conveniences such as importlib.reload() support for maturin projects.

Usage

After installing maturin, install the import hook into a python virtual environment with:

pip install maturin_import_hook

Then, optionally make the hook activate automatically with:

python -m maturin_import_hook site install

This only has to be run once for each virtual environment.

Alternatively, put the following at the top of each python script where you want to use the hook:

import maturin_import_hook

# install the import hook with default settings.
# this call must be before any imports that you want the hook to be active for.
maturin_import_hook.install()

# when a rust package that is installed in editable mode is imported,
# that package will be automatically recompiled if necessary.
import pyo3_pure

# when a .rs file is imported a project will be created for it in the
# maturin build cache and the resulting library will be loaded.
#
# assuming subpackage/my_rust_script.rs defines a pyo3 module:
import subpackage.my_rust_script

Once the hook is active, any import statement that imports an editable-installed maturin project will be automatically rebuilt if necessary before it is imported.

CLI

The package provides a CLI interface for getting information such as the location and size of the build cache and managing the installation into sitecustomize.py. For details, run:

python -m maturin_import_hook --help

Debugging

If you encounter a problem, a good way to learn more about what is going on is to enable the debug logging for the import hook. This can be done by putting the following lines above the import that is causing an issue:

# configure logging if you haven't already (make sure DEBUG level is visible)
logging.basicConfig(format="%(name)s [%(levelname)s] %(message)s", level=logging.DEBUG)
maturin_import_hook.reset_logger()

import some_package

License

Licensed under either of:

at your option.

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

maturin-import-hook-0.1.0.tar.gz (28.5 kB view details)

Uploaded Source

Built Distribution

maturin_import_hook-0.1.0-py3-none-any.whl (27.9 kB view details)

Uploaded Python 3

File details

Details for the file maturin-import-hook-0.1.0.tar.gz.

File metadata

  • Download URL: maturin-import-hook-0.1.0.tar.gz
  • Upload date:
  • Size: 28.5 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/5.0.0 CPython/3.12.2

File hashes

Hashes for maturin-import-hook-0.1.0.tar.gz
Algorithm Hash digest
SHA256 b31457d421db2a6aa51a2431a0f343ace17f4f59944a9670f2e6be66b3ea0499
MD5 9584e2c085db65039986529696a93227
BLAKE2b-256 efc76e111115358451e4dd65a83d7f47151cc177c219e58d544be53328073f4c

See more details on using hashes here.

Provenance

File details

Details for the file maturin_import_hook-0.1.0-py3-none-any.whl.

File metadata

File hashes

Hashes for maturin_import_hook-0.1.0-py3-none-any.whl
Algorithm Hash digest
SHA256 05cfeb40a161ec910081914aaf8909498b39b0e06d3edd89fd618b7cee38cceb
MD5 8d45df284181df661c11db2a1493092e
BLAKE2b-256 ddcbdebac4e82b2d0794488d638860a86ff7658cf5c4844e7519d09be4ed1e53

See more details on using hashes here.

Provenance

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page