How to create a pre-built Python extension from a Mojo package?

I’m trying to use a Mojo package as a pre-built Python extension inside a Python site‑package.

The goal is to compile the package into a .so file, place it in a specific mojo_libs folder, and then ship it with the Python site‑package so it can be imported at runtime.

The documentation on building Mojo extension modules covers single module examples:

But I’d like to apply the same idea to an entire package.

This is the initial project structure:
mojo_project/
├── pixi.lock
├── pixi.toml
└── src/
│ └── mathmojo/
│ ├── init.mojo
│ └── mojo_module.mojo

Attempt 1 – build directly from package folder:
pixi run mojo build src/mathmojo --emit shared-lib -o build/mathmojo.so

This doesn’t work. You can’t build from a package/folder:

error: cannot open ‘src/mathmojo’, since it does not appear to be a Mojo file (it does not end in ‘.mojo’ or ‘.🔥’)

Attempt 2 – build from packaged .mojopkg:

pixi run mojo package src/mathmojo -o build/mathmojo.mojopkg

This will package ‘mathmojo’ into a single .mojopkg file:

mojo_project
├── build
│ └── mathmojo.mojopkg
├── pixi.lock
├── pixi.toml
└── src
│ └── mathmojo
│ ├── init.mojo
│ └── mojo_module.mojo

This doesn’t work. You can’t build from a packaged file either:

pixi run mojo build build/mathmojo.mojopkg --emit shared-lib -o build/mathmojo.so

Error:

error: cannot open ‘build/mathmojo.mojopkg’, since it does not appear to be a Mojo file (it does not end in ‘.mojo’ or ‘.🔥’)

What’s the correct way to compile a Mojo package into a pre-built Python extension (.so) so it can be shipped inside a Python site‑package?

Is your goal to use this with Python projects? If that’s the case, you may not need to pre-compile this into a shared library. You could define a Python interface for your Mojo module, per the examples here, declare a dependency for your package on max, and on first invocation on the host system the Mojo module would be built into an architecture-specific shared library for you.

I’d like to be able to use Mojo packages from multiple other site-packages.
Ideally, I’d like to generate virtual environments with custom Python and Mojo site-packages, maybe using conda. I’d also like to be able to call between languages in any direction:
Mojo→Mojo, Python→Mojo, Mojo→Python, and Python→Python.

I’m sure that future versions will have these capabilities. For now, it would already be helpful if I just could compile Mojo packages into .so files, place them in a designated central folder, and call them from either Python or Mojo site-packages as required.

Is that currently possible? Do we have an example?