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?