Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

Conversation

@miili
Copy link

@miili miili commented Oct 27, 2025

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.

from awesome_package.extensions.mojo import factorial

factorial(123)

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.

  • Improved clarity, renamed helper functions and removed implicit comments.
  • Replaced assert with meaningful ImportError raises.
  • Implemented invalidate_caches, removing compiled Mojo extension.
    https://docs.python.org/3/library/importlib.html#importlib.invalidate_caches
  • Compiled files: Removed implicit hash- prefix from filename
  • Docs: Added documentation of module-level Python imports

Copilot AI review requested due to automatic review settings October 27, 2025 11:05
@miili miili requested a review from a team as a code owner October 27, 2025 11:05
@github-actions
Copy link

github-actions bot commented Oct 27, 2025

All contributors have signed the CLA ✍️ ✅
Posted by the CLA Assistant Lite bot.

Copy link
Contributor

Copilot AI left a 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 MojoImporter to support module-level imports (e.g., from package.submodule.mojo import function)
  • Implemented cache invalidation mechanism through invalidate_caches() method
  • Replaced assertions with proper ImportError exceptions for better error handling

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

mojo/python/mojo/importer.py Outdated Show resolved Hide resolved
mojo/python/mojo/importer.py Outdated Show resolved Hide resolved
@miili miili force-pushed the python-import-mojo-module branch 2 times, most recently from 1161fec to aab119d Compare October 27, 2025 11:07
@miili miili force-pushed the python-import-mojo-module branch from aab119d to 5dffbc9 Compare October 27, 2025 11:09
@miili
Copy link
Author

miili commented Oct 27, 2025

I have read the CLA Document and I hereby sign the CLA

@miili miili changed the title Enable Python Mojo module-level import [stdlib] Enable Python Mojo module-level import Oct 27, 2025
@miili miili requested a review from a team as a code owner October 27, 2025 11:49
modular-cla-bot bot added a commit to modular/cla that referenced this pull request Oct 27, 2025
@miili miili changed the title [stdlib] Enable Python Mojo module-level import [stdlib] Enable Python module-level Mojo imports Oct 27, 2025
@ConnorGray
Copy link
Collaborator

!sync

@modularbot modularbot added the imported-internally Signals that a given pull request has been imported internally. label Oct 31, 2025
@ConnorGray
Copy link
Collaborator

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 🙂

@ConnorGray
Copy link
Collaborator

!sync

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

imported-internally Signals that a given pull request has been imported internally.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants

Morty Proxy This is a proxified and sanitized view of the page, visit original site.