Cython Compilation Error: dynamic module does not define module export function

前端 未结 3 440
轮回少年
轮回少年 2020-12-06 05:22

I am building a package in Cython. I am using the following as the structure for setup.py:

from distutils.core import setup
from distutils.exten         


        
相关标签:
3条回答
  • 2020-12-06 05:37

    You need to call setup.py with Python 3 (python3 setup.py build_ext, maybe --inplace). It's because Python 3 defines a different name for the init function called when the module starts, and so you need to build it using Python 3 to ensure the correct name is generated.

    See dynamic module does not define init function (PyInit_fuzzy) and How to specify Python 3 source in Cython's setup.py? for slightly more detail (it's bordering on a duplicate of these questions, but isn't quite in my view)

    0 讨论(0)
  • 2020-12-06 05:49

    I had the same error for torchvision. FIxed it by downgrading the installation versions:

    pip install torch==1.2.0+cu92 torchvision==0.4.0+cu92 -f https://download.pytorch.org/whl/torch_stable.html
    
    0 讨论(0)
  • 2020-12-06 05:51

    I experienced this and found that I had to use the same name of .pyx as the module name, e.g.

    makefile:

    # (default)
    # INSTALL_DIR:=/usr/lib/python3.6/site-packages
    # (my venv)
    INSTALL_DIR:=/home/<username>/python3_venv/lib/python3.6/site-packages
    all:
        sudo python3 setup_myproj.py install --install-lib ${INSTALL_DIR}
    

    setup_myproj.py

    from distutils.core import setup, Extension
    from Cython.Build import cythonize
    
    ext = Extension("myproj",
                    sources=["myproj.pyx", "myCppProjFacade.cpp"],
                    <etc>
                    language="c++"
                   )
    
    setup(name="myproj",
          version="0.0.1",
          ext_modules=cythonize(ext))
    

    client module, run after installing to venv

    import myproj as myCppProjWrapper
    ...
    

    I also found that if the "myproj" names are different, under <python-lib-dir>/<python-vers>/site-packages the .so and .egg-info names are different and the client fails to load it.

    In addition I found that the client's environment does not need to have the cython package installed.

    0 讨论(0)
提交回复
热议问题