-
Notifications
You must be signed in to change notification settings - Fork 2.7k
[stdlib] Enable Python module-level Mojo imports #5514
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Conversation
|
All contributors have signed the CLA ✍️ ✅ |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Pull Request Overview
This PR enables module-level imports for Mojo code within Python packages by extending the MojoImporter class. It allows Python packages to ship with Mojo source code that can be compiled on-the-fly when imported, storing compiled extensions in a __mojocache__ directory within the module path.
Key Changes:
- Extended
MojoImporterto support module-level imports (e.g.,from package.submodule.mojo import function) - Implemented cache invalidation mechanism through
invalidate_caches()method - Replaced assertions with proper
ImportErrorexceptions for better error handling
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
1161fec to
aab119d
Compare
aab119d to
5dffbc9
Compare
|
I have read the CLA Document and I hereby sign the CLA |
|
!sync |
|
Hi @miili thank you so much for making this all around great contribution, especially from a first-time contributor! 🔥 This is a nice improvement to the UX of Python package authors, enabling them to better encapsulate their Mojo extensions. I've started the process of syncing this in to our internal sources. I may make a few minor changes as I integrate this, but all around this looks really good, and thank you again 🙂 |
|
!sync |
Adds module-level imports to
MojoImporter.This enables shipping Python packages with mojo code, where the package only needs a runtime dependency on the Mojo package.
The compiler and hashing logic are the same. The importer will create a
__mojocache__directory in the module path. I.e.<package_path>/autumn_package/extensions/mojo/__mojocache__/factorial.ae43a431b521.so.assertwith meaningfulImportErrorraises.invalidate_caches, removing compiled Mojo extension.https://docs.python.org/3/library/importlib.html#importlib.invalidate_caches
hash-prefix from filename