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).
The package we’ll share
Section titled “The package we’ll share”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, doublefrom .stats import meanbasic.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:
mkdir -p buildmojo precompile mathkit -o build/mathkit.mojocThis 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 packageUsing a .mojoc file
Section titled “Using a .mojoc file”In another project, put the file somewhere, for example in a libs folder:
app/├── libs/│ └── mathkit.mojoc└── main.mojomain.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:
mojo run -I libs main.mojoYou’ll see:
581.66666666666667Mojo 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 file name is the package name
Section titled “The file name is the package name”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 moduleA 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.
Tied to one compiler version
Section titled “Tied to one compiler version”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
.mojocfiles 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.
Where installed packages live
Section titled “Where installed packages live”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.
Publishing a package for others
Section titled “Publishing a package for others”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:
- Put your code in a public Git repository, for example with the package in
src/mathkit/and atest.mojothat imports it. - Write a recipe,
conda.recipe/recipe.yaml, that says where the source is, how to build it and how to test it. - 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.mojoThe 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/mojois the folder the compiler searches automatically, so people who install the package canimport mathkitwithout-I.- Pin
mojo-compilerto the version you built with (we checked that1.2.0is on Modular’s conda channel), because of the version rule above.pin_compatiblemakes 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.
Practice
Section titled “Practice”1. Precompile and use
Section titled “1. Precompile and use”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
mkdir -p distmojo precompile text -o dist/text.mojocmkdir -p ../democp dist/text.mojoc ../demo/../demo/main.mojo:
from text import shout, whisper
def main(): print(shout("packed")) print(whisper("SHIPPED"))cd ../demomojo run main.mojoYou’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.
2. A new name
Section titled “2. A new name”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:
mojo precompile text -o dist/fancytext.mojocThen 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.mojocturns a package folder (with__init__.mojo) into one file.mojo packageand.mojopkgare the old names.- Use a
.mojoclike a source package: next to your file or through-I. - The
-oname is the package name; don’t rename the file later, since imports inside the package keep the old name. - A
.mojocworks 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
Section titled “Quick check”Quick check
Read more
Section titled “Read more”- Packaging: the official guide to recipes, rattler-build and the modular-community channel.
- mojo precompile: the command and its options.
- Modules and packages: packages,
__init__.mojoand precompiled files in the manual. - modular-community on GitHub: where community recipes are submitted and built.
- rattler-build recipe reference: every field you can put in
recipe.yaml.