#4064·Nuitka

Package/wheel mode skips Py_mod_exec for installed PEP 489 extension submodules

Author: mablCreated Sep 25, 2026Updated Sep 28, 2026
Labelsbugfactoryexcellent_report

Bug Report

Bug Description

A wheel built with nuitka.distutils.Build can contain a successfully compiled Python package and a separately installed PEP 489 native extension beneath it, but importing the native extension does not execute its Py_mod_exec slot. Import succeeds with only module metadata present; functions and constants added by the execution slot are missing.

The standalone reproducer below has one small Python package and one C extension. It has no application dependencies and does not require Cython or NumPy. The ordinary CPython/setuptools wheel passes. The Nuitka wheel fails on 4.2.2 and current develop, 4506e7927f39e4c58146ee5906c8f10c579f1729 (4.3rc3).

️ Environment

1. Nuitka Version, Python Version, OS, and Platform

Output of python -m nuitka --version; only the absolute executable path has been replaced with a neutral placeholder.

Stable:

4.2.2
Update status: up to date with stable release '4.2.2' (cached, 42 minutes old).
Commercial: None
Python: 3.13.11 (main, Dec  9 2025, 19:04:10) [Clang 21.1.4 ]
Flavor: Python Build Standalone
GIL: yes
Executable: <reproducer>/.venv/bin/python
OS: Linux
Arch: x86_64
Distribution: Ubuntu (based on Debian) 24.04.4
Version C compiler: gcc (gcc 13).

Develop:

4.3rc3
Update status: newer than stable release '4.2.2' (cached, 44 minutes old).
Commercial: None
Python: 3.13.11 (main, Dec  9 2025, 19:04:10) [Clang 21.1.4 ]
Flavor: Python Build Standalone
GIL: yes
Executable: <reproducer>/.venv/bin/python
OS: Linux
Arch: x86_64
Distribution: Ubuntu (based on Debian) 24.04.4
Version C compiler: gcc (gcc 13).

2. How Nuitka and Python were Installed

Python 3.13.11 was provisioned by uv into a fresh virtual environment on Ubuntu under WSL. Nuitka 4.2.2 was installed from PyPI. Develop was installed from the immutable GitHub source archive linked in the commands below. The native extension and Nuitka-generated code were built with GCC 13.3.

3. Relevant PyPI Packages and Versions

python -m nuitka --list-distribution-metadata before installing the stable reproducer wheel:

Nuitka 4.2.2 uv
setuptools 84.0.0 uv

After installing the reproducer and switching to develop:

Nuitka 4.3rc3 uv
pep489-wheel-repro 0.0.1 uv
setuptools 84.0.0 uv

️ To Reproduce

1. "Hello World" Test (if applicable)

Wheel compilation succeeds. The compiled package's pure-Python hello() function returns "hello world" successfully, including in the failing runs below. The failure is specific to initialization of its native child module. The same check passes completely with the ordinary CPython/setuptools wheel.

2. Short, Self-Contained, Correct, Eligible (SSCCE) Example

Create these four files:

pyproject.toml
check.py
src/pep489_repro/__init__.py
src/pep489_repro/native.c

pyproject.toml:

toml
[build-system]
requires = ["setuptools>=80", "nuitka>=4.2.2"]
build-backend = "nuitka.distutils.Build"

[project]
name = "pep489-wheel-repro"
version = "0.0.1"
requires-python = ">=3.13"

[tool.setuptools.packages.find]
where = ["src"]

[[tool.setuptools.ext-modules]]
name = "pep489_repro.native"
sources = ["src/pep489_repro/native.c"]

src/pep489_repro/__init__.py:

python
def answer():
    from . import native

    return native.answer()


def hello():
    return "hello world"

src/pep489_repro/native.c:

c
#define PY_SSIZE_T_CLEAN
#include <Python.h>

static PyObject *answer(PyObject *self, PyObject *unused) {
    return PyLong_FromLong(42);
}

static PyMethodDef methods[] = {
    {"answer", answer, METH_NOARGS, NULL},
    {NULL, NULL, 0, NULL}
};

static int execute(PyObject *module) {
    if (PyModule_AddFunctions(module, methods) < 0) {
        return -1;
    }
    return PyModule_AddIntConstant(module, "exec_called", 1);
}

static PyModuleDef_Slot slots[] = {
    {Py_mod_exec, execute},
    {0, NULL}
};

static struct PyModuleDef definition = {
    PyModuleDef_HEAD_INIT,
    .m_name = "pep489_repro.native",
    .m_size = 0,
    .m_slots = slots
};

PyMODINIT_FUNC PyInit_native(void) {
    return PyModuleDef_Init(&definition);
}

check.py:

python
import sys

import pep489_repro
import pep489_repro.native as native

print("Python package function:", pep489_repro.hello())
print("Extension loader:", native.__loader__)
print("Extension attributes:", sorted(vars(native)))
print("exec_called:", getattr(native, "exec_called", None))
assert native.exec_called == 1
assert sys.modules["pep489_repro.native"] is native
assert pep489_repro.native is native
assert pep489_repro.answer() == 42
print("PASS")

Run from the reproducer directory on Linux:

bash
uv venv --python 3.13 .venv
uv pip install --python .venv/bin/python 'setuptools==84.0.0' 'nuitka==4.2.2'

# Positive control: ordinary CPython package plus native extension.
uv build --python .venv/bin/python --no-build-isolation --wheel \
  -Cbuild_with_nuitka=false --out-dir dist-cpython
uv pip install --reinstall --python .venv/bin/python \
  dist-cpython/pep489_wheel_repro-0.0.1-cp313-cp313-linux_x86_64.whl
.venv/bin/python -B check.py
# PASS

# Nuitka wheel: compilation succeeds, installed-package check fails.
uv build --python .venv/bin/python --no-build-isolation --wheel \
  --out-dir dist-nuitka
uv pip install --reinstall --python .venv/bin/python \
  dist-nuitka/pep489_wheel_repro-0.0.1-cp313-cp313-linux_x86_64.whl
.venv/bin/python -B check.py
# AttributeError: module 'pep489_repro.native' has no attribute 'exec_called'

The --no-build-isolation option deliberately selects the compiler installed in this environment. To test the current develop revision, install it and repeat the Nuitka build/install/check:

bash
uv pip install --python .venv/bin/python \
  'nuitka @ https://github.com/Nuitka/Nuitka/archive/4506e7927f39e4c58146ee5906c8f10c579f1729.tar.gz'
.venv/bin/python -m nuitka --version

3. Nuitka Command Line Options

No custom [tool.nuitka] options are needed to reproduce the failure. The wheel backend generates this invocation; paths below are expressed relative to the reproducer directory:

bash
.venv/bin/python -m nuitka \
  --mode=package \
  --enable-plugin=pylint-warnings \
  --output-dir=build/lib.linux-x86_64-cpython-313 \
  '--nofollow-import-to=*.tests' \
  --nowarn-mnemonic=compiled-package-hidden-by-package \
  --remove-output \
  --report=compilation-report.xml \
  build/lib.linux-x86_64-cpython-313/pep489_repro

These are the backend's defaults, not hand-added diagnostic exclusions. It warns that pep489_repro.native cannot be included in a non-standalone compilation. The wheel nevertheless contains the separately built pep489_repro/native.cpython-313-x86_64-linux-gnu.so, which imports correctly in the CPython control.

Expected Behavior

Importing the native extension executes its Py_mod_exec slot before the import returns. native.exec_called == 1, pep489_repro.answer() == 42, and the module identity assertions pass.

Actual Behavior & Output

Both stable and develop produce:

Python package function: hello world
Extension loader: <class 'nuitka_module_loader'>
Extension attributes: ['__doc__', '__file__', '__loader__', '__name__', '__package__', '__spec__']
exec_called: None

The next assertion raises:

AttributeError: module 'pep489_repro.native' has no attribute 'exec_called'

The extension's answer function is likewise absent. The CPython control prints exec_called: 1, includes answer in the attributes, and finishes with PASS.

↩️ Regression (if applicable)

I have not established the first affected or last working version. The default loading path fails on 4.2.2 and develop at the revision above.

Additional Context (Optional)

Experimental old-loader comparison

An earlier equivalent minimal PEP 489 C reproducer passed on both 4.2.2 and the official 4.3rc3 archive with this build option:

toml
[tool.nuitka]
experimental = "old-extension-module-loading"

The generated compiler command includes --experimental=old-extension-module-loading; normal imports, the execution marker, function result, and module identity all passed for that earlier equivalent reproducer. I understand this is experimental and am not assuming it is a supported production remedy. Is it appropriate as a temporary setting for this case?

Possible missing normal-loader path

In MetaPathBasedLoader.c on 4.2.2:

  • The normal create_module path finds the separately installed extension through installed_extension_modules and calls callIntoInstalledExtensionModule().
  • For a multi-phase extension, PyModule_FromDefAndSpec() creates the module, with execution deferred in the normal path.
  • The normal exec_module path calls executeExtensionModuleDef() for a compiled-in loader entry carrying NUITKA_EXTENSION_MODULE_FLAG. The separately installed-extension branch there is enabled only for the old experimental loader.

This seems to leave extensions discovered via installed_extension_modules without their execution phase in the normal package/wheel case. The inspected develop source has the same distinction.

I searched existing issues and found related reports, but not this exact case:

  • #2833: creation/execution split for lazy extension imports; closed as fixed in 2.7.
  • #3798: multi-phase/post-load handling in standalone/PySide; closed as fixed in 4.1.
  • #1242: historical mixed compiled/native imports outside standalone mode.

This report is about an importable package wheel rather than a standalone executable.