Is that possible to generate __pyx_CommonTypesMetaclass_get_module into a single file, instead of in every module?

25 views
Skip to first unread message

J Wu

unread,
Jul 16, 2025, 5:06:06 AMJul 16
to cython-users
Hi! I am trying to use Cython to compile my py files into c files, and then compile and link them in to a standalone binary. I created a toy project below to elaborate what I am doing.

The issue I have now is that Cython generates the __pyx_CommonTypesMetaclass_get_module function in every c files. So that when I link the objects, I got "multiple definition" error. I have tried manually changing one of the definition to be extern in a generated c file, which fixed the issue.

I am wondering if there is a way that we can generate the function into a separated file, to avoid this issue? Thank you very much!

Best regards,
J


The toy project here in case you have interests to look into.
Project structure:

test
├── build
│   └── test
├── build.sh
├── main.c
└── test
    ├── common
    │   └── util.py
    └── hello.py

----------------------------------------------------------------------------------------------------------------------------------------
build.sh:
--------------------------------------------------

#!/bin/bash

set -e


#Cythonize manually

cython -3 -o build/test/hello.c test/hello.py

cython -3 -o build/test/common/util.c test/common/util.py


gcc -c -Os -I /usr/include/python3.13 -o build/test/hello.o build/test/hello.c

gcc -c -Os -I /usr/include/python3.13 -o build/test/common/util.o build/test/common/util.c

gcc -c -Os -I /usr/include/python3.13 -o build/main.o main.c


# Build 

gcc -Os -o hello build/main.o build/test/hello.o build/test/common/util.o -lpython3.13 -lpthread -lm -lutil -ldl 

--------------------------------------------------------------------------------------------------------------------------------------------
main.c:
----------------------------------

#include <Python.h>


extern PyObject* PyInit_hello(void);

extern PyObject* PyInit_util(void);


int main(int argc, char *argv[]) {

    PyImport_AppendInittab("hello", PyInit_hello);

    PyImport_AppendInittab("util", PyInit_util);

    Py_Initialize();


    PyObject *util_mod = PyImport_ImportModule("util");

    PyDict_SetItemString(PyImport_GetModuleDict(), "test.common.util", util_mod);


    PyObject *mod = PyImport_ImportModule("hello");

    if (!mod) {

        PyErr_Print();

        return 1;

    }


    Py_DECREF(util_mod);

    Py_DECREF(mod);


    Py_Finalize();

    return 0;

}

----------------------------------------------------------------------------------------------------------------------------------

test/hello.py:

--------------------------------------------------------

import pandas as pd

from test.common.util import helper


def test():

    d = {'col1': [1, 2], 'col2': [3, 4]}

    df = pd.DataFrame(data=d)

    print(df)


print("hello world")

test()

helper()

------------------------------------------------------------------------------------------------------------------------------------

test/common/util.py:

--------------------------------------

def helper():

    print("help")

-------------------------------------------------------------------------------------------------------------------------------------


David Woods

unread,
Jul 16, 2025, 6:57:40 AMJul 16
to cython...@googlegroups.com
Hi,

It's a known bug. It's been fixed and will be in the next release. Sorry!

Reply all
Reply to author
Forward
0 new messages