2025, Dec 15 19:00

Hunting down Nuitka onefile 'No module named' errors on Linux Mint 22: install missing dependencies like cryptography in the venv

Building a Nuitka onefile fails with 'No module named'? Install missing packages like cryptography in the venv and rebuild for a working executable reliably.

Packaging a Python script into a single executable can be deceptively simple until a dependency quietly slips through the cracks. A typical case: building a onefile binary with Nuitka on Linux Mint 22 inside a virtual environment succeeds, but at runtime the executable fails with a “No module named …” error. The build uses Python 3.12.3 and Nuitka 2.7.2, compiles without visible issues, and produces the .bin file along with build directories. The error appears as soon as the program hits the second import.

Minimal example that reproduces the failure

The script’s header looks like this, and the failure occurs on the second import:

#!/usr/bin/python3
from pathlib import Path as Pth
from cryptography.fernet import Fernet as FernetCipher
import subprocess as sh
import math as maths
from math import gcd as gcd_fn

The build and run steps are straightforward:

python3 -m nuitka --standalone --follow-imports --python-flag=no_site --onefile myscript.py
./myscript.bin

The binary starts, then aborts with “No module named …” right where the import of cryptography should succeed.

What’s actually going on

The root cause is environmental. Native modules differ between the system environment and the virtual environment used for the build. In this case, cryptography is available outside the virtual environment but not installed inside it. The build was performed from within the virtual environment, so that missing dependency never made it into the produced binary. As a result, the executable fails at runtime when it tries to import cryptography.

This isn’t specific to Nuitka as a toolchain quirk. The same behavior shows up when attempting similar builds under the same conditions with PyInstaller or cx_Freeze: if a module isn’t present in the virtual environment you build from, it won’t be packaged.

Fix: install the missing dependency in the venv, then rebuild

The remedy is to make sure that the dependency is installed in the exact environment used to compile the binary. After installing cryptography inside the virtual environment, rebuilding with the same command produces a working executable. The final binary runs even on a machine that has neither Python nor cryptography installed.

pip install cryptography
python3 -m nuitka --standalone --follow-imports --python-flag=no_site --onefile myscript.py
./myscript.bin

Why you want this on your radar

Executable builders rely on what they can see in the build environment. If your virtual environment doesn’t mirror what your script needs, the failure won’t be obvious during compilation; it will surface when the binary starts importing modules at runtime. Knowing this helps avoid puzzling “No module named …” errors that appear inconsistent with how the script runs under the system interpreter.

Takeaways

When creating a single-file, standalone executable with Nuitka on Linux Mint 22 in a virtual environment, ensure that every third-party module your code imports is installed in that same virtual environment. In the example above, installing cryptography in the venv and rebuilding resolved the issue. The Python and Nuitka versions matched (Python 3.12.3 with Nuitka 2.7.2), so the problem wasn’t a version mismatch; it was the absence of the required package in the build environment.

A practical habit is to align your development and build environments and then test the produced binary in a clean environment. If the binary launches and imports successfully there, you’ve captured the dependencies correctly.