2

How do I import a C module in python that was generated with Cython?

I have tried the normal way:

from app.CythonOperations import executor_unpack

Could this be that python3 may not be able to recognize cpdef or cdef functions in C? When running it locally, this gives me no issues. When deploying to the Okteto Cloud, the logs say:

ImportError: cannot import name 'executor_unpack' from 'app.CythonOperations' (unknown location)

This is my executor_unpack.pyx file:

cpdef executor_read(list results):
    cdef list response_packet = [[], []]
    cdef list data = []
    cdef list supplied = []
    cdef dict d = {}
    cdef dict s = {}

    for result in results:
        data = result[0]
        supplied = result[1]
        for d, s in zip(data, supplied):
            response_packet[0].append(d)
            response_packet[1].append(s)

    return response_packet

I also have the executor_unpack.c file present in CythonOperations directory. The executor_unpack.pyx file is not in the same directory. I am currently deploying this software and would appreciate anyone's thoughts on how to fix this issue :) Thanks.

  • 1
    `cdef` functions should not be importable in Python - that is the point of them. (`cpdef` functions should be). Have you compiled the .c file to a .so file? It sounds like you haven't – DavidW Aug 24 '21 at 10:41
  • The functions are all cpdef functions. I apologize if it was unclear before. I have both the .c files and .so files compiled. It works when tested on my machine but when deployd to the Okteto Cloud it begins to cause the aforementioned issues. – Martin Mashalov Aug 24 '21 at 11:10
  • 1
    You may need to recompile it on your cloud? The compiled .so file is not *very* redistributable (relies on same OS, same Python version, same processor, etc) – DavidW Aug 24 '21 at 11:16
  • Does this answer your question? [Importing cython generated \*.so-module with another python-version or on another OS](https://stackoverflow.com/questions/65597206/importing-cython-generated-so-module-with-another-python-version-or-on-another) – ead Aug 24 '21 at 11:48
  • How do I recompile the .so files on the cloud? Any docs would be really helpful. Thank you. – Martin Mashalov Aug 24 '21 at 13:32

0 Answers0