# PyMODINIT\_FUNC

**URL:** <https://ziggit.dev/t/pymodinit-func/3719>\
**Category:** Help\
**Tags:** c\
**Created:** [March 27, 2024, 8:10pm UTC](https://ziggit.dev/t/pymodinit-func/3719 "2024-03-27T20:10:00Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![glitch](https://ziggit.dev/user_avatar/ziggit.dev/glitch/32/2068_2.png) [@glitch](https://ziggit.dev/u/glitch)\
**Post date:** [March 27, 2024, 8:10pm UTC](https://ziggit.dev/t/pymodinit-func/3719/1 "2024-03-27T20:10:00Z")

</div>

Hi there!

I’m experimenting with different tools to write low level extensions to Python. I’ve written a bunch of extensions with PyBind11, but I really wanted to mess with Zig. I’ve been looking for an excuse for ages to try it.

Anyway, if you’re in C code, you register an extension module by using this PyMODINIT\_FUNC thing like this:

C:

```zig
PyMODINIT_FUNC PyInit_simple(void) {
    return PyModule_Create(&module);
}

```

Zig:

```zig
pub export fn PyInit_simple() PyMODINIT_FUNC {
    return PyModule_Create(&module);
}

```

When compiling, I get an error:

```zig
pub const PyMODINIT_FUNC = @compileError("unable to translate C expr: unexpected token 'Eof'"); 

```

I’m so new to Zig that I don’t even know where to start. Did I bite off more than I could chew? Should I just go back to my previous Zigless (boring) life with C++ / PyBind11?

Also first time posting, really hope it supports markdown triple backtick.

Cheers everyone \<3

---

<div class="post-metadata">

**Author:** ![dimdin](https://ziggit.dev/user_avatar/ziggit.dev/dimdin/32/1457_2.png) [@dimdin](https://ziggit.dev/u/dimdin)\
**Post date:** [March 27, 2024, 8:29pm UTC](https://ziggit.dev/t/pymodinit-func/3719/2 "2024-03-27T20:29:25Z")

</div>

Hi @glitch, welcome to ziggit 🙂

`cImport` generates a `@compileError` when it cannot parse the definition.  
Since PyMODINIT\_FUNC is actually a pointer to a PyObject, the following code must work:

```zig
const py = @cImport({
    @cDefine("PY_SSIZE_T_CLEAN", 1);
    @cInclude("Python.h");
});

const PyObject = py.PyObject;

pub export fn PyInit_simple() *PyObject {
    return PyModule_Create(&module);
}

```

---

<div class="post-metadata">

**Author:** ![glitch](https://ziggit.dev/user_avatar/ziggit.dev/glitch/32/2068_2.png) [@glitch](https://ziggit.dev/u/glitch)\
**Post date:** [March 27, 2024, 8:42pm UTC](https://ziggit.dev/t/pymodinit-func/3719/3 "2024-03-27T20:42:44Z")

</div>

OK will check it out thank you \<3

---

<div class="post-metadata">

**Author:** ![glitch](https://ziggit.dev/user_avatar/ziggit.dev/glitch/32/2068_2.png) [@glitch](https://ziggit.dev/u/glitch)\
**Post date:** [March 27, 2024, 8:57pm UTC](https://ziggit.dev/t/pymodinit-func/3719/4 "2024-03-27T20:57:04Z")

</div>

@dimdin Thank you so much, that helped me compile the program.

Weirdly I’m getting an error now on the Python side of things, which I recognize isn’t necessarily relevant subject matter for this forum.

Here is my whole program at this point:

```const
    @cDefine("PY_SSIZE_T_CLEAN", {});
    @cInclude("Python.h");
});

const std = @import("std");
const print = std.debug.print;

const PyObject = py.PyObject;
const PyMethodDef = py.PyMethodDef;
const PyModuleDef = py.PyModuleDef;
const Py_BuildValue = py.Py_BuildValue;
const PyModule_Create = py.PyModule_Create;
const PyArg_ParseTuple = py.PyArg_ParseTuple;

fn do_stuff(self: [*c]PyObject, args: [*c]PyObject) callconv(.C) [*]PyObject {
    _ = self;
    var input: [*:0]u8 = undefined;
    if (PyArg_ParseTuple(args, "s", &input) == 0) return Py_BuildValue("");
    print("you entered: {s}\n", .{input});
    return Py_BuildValue("s", "Take off every Zig");
}

var methods = [_]PyMethodDef{
    PyMethodDef{
        .ml_name = "do_stuff",
        .ml_meth = do_stuff,
        .ml_flags = @as(c_int, 1),
        .ml_doc = "For Great Justice!",
    },
};

var module = PyModuleDef{
    .m_name = "simple",
    .m_methods = &methods,
};

pub export fn PyInit_simple() *PyObject {
    return PyModule_Create(&module);
}

```

Trying to import the module in Python gives me a very odd looking error:

`ImportError: dynamic module does not define module export function (PyInit_simple)`

It must mean that there is something off about the type signature of the `PyInit_simple` fn?

Curious if anyone has seen this.

Thanks again \<3

---

<div class="post-metadata">

**Author:** ![dimdin](https://ziggit.dev/user_avatar/ziggit.dev/dimdin/32/1457_2.png) [@dimdin](https://ziggit.dev/u/dimdin)\
**Post date:** [March 27, 2024, 9:15pm UTC](https://ziggit.dev/t/pymodinit-func/3719/5 "2024-03-27T21:15:15Z")

</div>

Some ideas:

- PyInit\_simple must be in a module called simple (not sure if this is libsimple.so/simple.dll or something else)
- Where python is trying to find this synamic library (libsimple.so/simple.dll or whatever must be called).

---

<div class="post-metadata">

**Author:** ![AndrewCodeDev](https://ziggit.dev/letter_avatar_proxy/v4/letter/a/278dde/32.png) [@AndrewCodeDev](https://ziggit.dev/u/AndrewCodeDev)\
**Post date:** [March 27, 2024, 9:16pm UTC](https://ziggit.dev/t/pymodinit-func/3719/6 "2024-03-27T21:16:57Z")

</div>

> [@glitch](#):
>
> `ImportError: dynamic module does not define module export function (PyInit_simple)`

Yeah, when I used to use `pybind` the issue is that the names were off. Here’s a stack overflow that may help. Looks like you may need to rename something: [python - Unable to solve "ImportError: dynamic module does not define module export function" - Stack Overflow](https://stackoverflow.com/questions/55277328/unable-to-solve-importerror-dynamic-module-does-not-define-module-export-funct)

Also, welcome @glitch, good to see some python developers making their way onto the forum!

---

<div class="post-metadata">

**Author:** ![Sze](https://ziggit.dev/user_avatar/ziggit.dev/sze/32/496_2.png) [@Sze](https://ziggit.dev/u/Sze)\
**Post date:** [March 27, 2024, 10:59pm UTC](https://ziggit.dev/t/pymodinit-func/3719/7 "2024-03-27T22:59:45Z")

</div>

I don’t have direct answers, but here is a meetup talk and the related project:

[![](https://ziggit.dev/uploads/default/original/2X/4/492d3f08f421ef11179a99b4d6c0bfc80859f5e7.jpeg "Using Zig to write native extension modules for Python - Adam Serafini") ](https://www.youtube.com/watch?v=AKhqFOdjUfg)

> **[GitHub - adamserafini/zaml: 🚀 Fast YAML 1.2 parsing library for Python 3](https://github.com/adamserafini/zaml)**
>
> 🚀 Fast YAML 1.2 parsing library for Python 3. Contribute to adamserafini/zaml development by creating an account on GitHub.

---

<div class="post-metadata">

**Author:** ![glitch](https://ziggit.dev/user_avatar/ziggit.dev/glitch/32/2068_2.png) [@glitch](https://ziggit.dev/u/glitch)\
**Post date:** [March 28, 2024, 1:19pm UTC](https://ziggit.dev/t/pymodinit-func/3719/8 "2024-03-28T13:19:22Z")

</div>

Thank you so much for the warm welcome!

I’ll keep poking away and seeing if I can get my Python ext module boilerplate to do stuff. Folks clearly have linked me working examples so it’s probably just a PEBKAC error…

It would be super nice to be able to use Zig. I love the syntax, it’s so much cleaner and more modern feeling than C…

Also, even if I fail in my task and I’m stuck in C-land I absolutely intend to utilize Zig’s cross-platform compiler tooling. That right there is downright amazing.

\<3
