Skip to content

Mojo Packaging

In Mojo Modules & Packages you split code into a package folder. That’s enough inside one project. But what about using the same code in another project, or giving it to other people? This page covers the two steps Mojo offers: turning a package into a single .mojoc file with mojo precompile, and publishing it as a conda package that others can install.

Commands and output were checked with Mojo 1.2 on Linux, except the publishing part at the end, which follows the official guide (we didn’t publish a package).

We’ll reuse the mathkit package from the modules lesson:

mylib/
└── mathkit/
├── __init__.mojo
├── basic.mojo
└── stats.mojo

__init__.mojo makes the important names available from the package itself:

from .basic import add, double
from .stats import mean

basic.mojo has add and double, and stats.mojo has mean, exactly as in the modules lesson. The simplest way to share this is to copy the folder into the other project, or keep it in a Git repository and point to it with -I. That works well and lets people read the code. Precompiling is the next step up.

mojo precompile: one file instead of a folder

Section titled “mojo precompile: one file instead of a folder”

From the mylib folder, run:

Terminal window
mkdir -p build
mojo precompile mathkit -o build/mathkit.mojoc

This writes one file, build/mathkit.mojoc (about 43 KB here). It holds the whole package in a form the compiler can load faster than source files. Without -o, you get mathkit.mojoc in the current folder. As with mojo build, the output folder must already exist; otherwise you’ll see cannot open output file 'build/mathkit.mojoc': No such file or directory.

The folder must be a real package. Without __init__.mojo you get:

mojo: error: 'mathkit' does not correspond to a Mojo package

In another project, put the file somewhere, for example in a libs folder:

app/
├── libs/
│ └── mathkit.mojoc
└── main.mojo

main.mojo imports from it exactly as if the source folder were there:

from mathkit import add, mean
def main():
var scores: List[Int] = [70, 85, 90]
print(add(2, 3))
print(mean(scores))

Tell Mojo where the file is with -I:

Terminal window
mojo run -I libs main.mojo

You’ll see:

5
81.66666666666667

Mojo searches the folders the same way for .mojoc files as for source packages: next to the file you run, and in every -I folder. So if mathkit.mojoc sits right next to main.mojo, you don’t need -I at all. mojo build -I libs main.mojo works the same way, and the built program doesn’t need the .mojoc file afterwards.

The name you give with -o becomes the package name. mojo precompile mathkit -o build/numtools.mojoc makes a package you import with from numtools import add, mean.

Don’t rename the file afterwards, though. The manual says the package name is stored inside the file, and our tests showed what that means. After renaming numtools.mojoc to renamed.mojoc, from renamed import add still worked, but from renamed import mean failed, because stats.mojo inside the package imports its neighbor with from .basic import add, and that import still looks for the old name:

<unknown>:0: error: unable to locate module 'numtools'
mojo: error: failed to parse the provided Mojo source module

A renamed file that half works is worse than one that fails right away. If you want a different name, run mojo precompile again with a new -o.

A .mojoc file isn’t machine code yet; the code becomes machine code when a program that imports it is built. So the same file works on Linux and macOS. The catch, according to the manual’s page on packages: it only works with the exact Mojo version that made it. Loading it with another version is a compile error. (We only had Mojo 1.2 to test with, so we didn’t see this error ourselves.)

In practice:

  • Treat .mojoc files like build output: don’t commit them, rebuild them when you upgrade Mojo.
  • If you hand one to someone, tell them which Mojo version to use, and pin that version in their project (uv add "mojo==1.2.0").
  • For anything long-lived, share the source too. The source keeps working across versions far more often than a precompiled file.

The Mojo compiler also looks in a fixed folder inside your environment, the same place its own standard library (std.mojoc) lives. In a uv project that’s .venv/lib/python3.13/site-packages/modular/lib/mojo/ (the Python version may differ). In our test, a .mojoc copied there was imported without any -I. That’s how installed packages work: a conda package puts its .mojoc file into that kind of folder.

Don’t copy files into .venv by hand, though. uv owns that folder and can replace it at any time. Use -I for your own packages, and let a package manager handle the folder.

To let other people install your package with one command, Mojo uses conda packages, built with a tool called rattler-build. The official packaging guide walks through it. In short:

  1. Put your code in a public Git repository, for example with the package in src/mathkit/ and a test.mojo that imports it.
  2. Write a recipe, conda.recipe/recipe.yaml, that says where the source is, how to build it and how to test it.
  3. Build it with rattler-build, or open a pull request to the modular-community repository, which builds and hosts community packages for you.

The heart of the recipe is the build step, which is just mojo precompile writing into the package’s lib/mojo folder:

build:
number: 0
script:
- mojo precompile src/mathkit -o ${{ PREFIX }}/lib/mojo/mathkit.mojoc
requirements:
build:
- mojo-compiler =1.2.0
host:
- mojo-compiler =1.2.0
run:
- ${{ pin_compatible('mojo-compiler') }}
tests:
- script:
- mojo run test.mojo
files:
recipe:
- test.mojo

The full recipe also has package (name and version), source (your Git repository and a full commit hash) and about (license, homepage, summary). The guide has a complete template. Two things matter most:

  • $PREFIX/lib/mojo is the folder the compiler searches automatically, so people who install the package can import mathkit without -I.
  • Pin mojo-compiler to the version you built with (we checked that 1.2.0 is on Modular’s conda channel), because of the version rule above. pin_compatible makes installs pick a matching compiler.

People then install your package with pixi (or another conda tool) after adding the https://repo.prefix.dev/modular-community channel to their project. There’s no route through uv or PyPI for Mojo packages in the official guide.

Take the text package from the modules practice (with shout and whisper), precompile it into dist/text.mojoc, and use it from a main.mojo in a different folder.

Hint: create dist first, and pass -I with the folder that holds the .mojoc file.

Show solution
Terminal window
mkdir -p dist
mojo precompile text -o dist/text.mojoc
mkdir -p ../demo
cp dist/text.mojoc ../demo/

../demo/main.mojo:

from text import shout, whisper
def main():
print(shout("packed"))
print(whisper("SHIPPED"))
Terminal window
cd ../demo
mojo run main.mojo

You’ll see:

PACKED!
shipped...

No -I is needed here, because text.mojoc sits next to main.mojo. If you’d kept it in another folder, you’d add -I that-folder.

You want the same package to be importable as fancytext. What’s the right way to do it, and what goes wrong if you just rename text.mojoc?

Hint: where does the package name come from?

Show solution

Build it again with the new name:

Terminal window
mojo precompile text -o dist/fancytext.mojoc

Then from fancytext import shout works. Renaming text.mojoc to fancytext.mojoc isn’t safe: the old name text is stored inside the file. Our small text package happened to survive a rename, but a package whose modules import each other breaks with unable to locate module 'text', as shown above.

  • The simplest way to share is the source folder (copy it, or a Git repository plus -I).
  • mojo precompile pkg -o name.mojoc turns a package folder (with __init__.mojo) into one file. mojo package and .mojopkg are the old names.
  • Use a .mojoc like a source package: next to your file or through -I.
  • The -o name is the package name; don’t rename the file later, since imports inside the package keep the old name.
  • A .mojoc works only with the Mojo version that built it: rebuild after upgrades and pin versions.
  • Public packages are conda packages built with rattler-build, usually through the modular-community channel, installed with pixi.

Next up: Mojo random numbers, dice, shuffles and repeatable results.

Quick check

1. Which command turns the package folder geo into a file you can import as geo?
2. You rename geo.mojoc to shapes.mojoc and import from shapes. What should you expect?
3. A colleague on a different Mojo version can't load your .mojoc file. Why?
4. Where does a conda package for a Mojo library put its .mojoc file so import finds it without -I?