Python Cython 编译错误:动态模块未定义模块导出功能

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/36723206/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-19 18:16:16  来源:igfitidea点击:

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

pythonnumpycython

提问by Alger Remirata

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

我正在 Cython 中构建一个包。我使用以下作为结构setup.py

from distutils.core import setup
from distutils.extension import Extension
from Cython.Build import cythonize
import numpy
import scipy

extensions = [
    Extension("xxxxx",["xxxx/xxxxx.pyx"],
    include_dirs=[numpy.get_include(),"."]),
    Extension("nnls",["xxxxx/xxxxx.pyx"],
              include_dirs=[numpy.get_include(),"."]),
]

setup(
    name='xxxxxx',
    version='0.0.0',
    description='''********''',
    url='xxxxxxx',
    author='xxxxx',
    author_email='xxxxx',
    packages=[
        'xxxxx',
    ],
    install_requires=[
        'cython',
        'numpy',
        'scipy',
    ],
    ext_modules=cythonize(extensions),
)

However, I am getting an error upon installation in Python 3. It is working in Python 2 however, it is not compiling in Python 3 having the following error:

但是,我在 Python 3 中安装时遇到错误。它在 Python 2 中工作,但是,它没有在 Python 3 中编译,出现以下错误:

dynamic module does not define module export function

动态模块没有定义模块导出功能

How can I solve this problem? Is the structure of the setup.pythe reason why this is not compiling?

我怎么解决这个问题?setup.py这是不是编译的原因的结构吗?

采纳答案by DavidW

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 initfunction called when the module starts, and so you need to build it using Python 3 to ensure the correct name is generated.

您需要使用 Python 3 ( python3 setup.py build_ext,也许--inplace)调用 setup.py 。这是因为 Python 3 为init模块启动时调用的函数定义了不同的名称,因此您需要使用 Python 3 构建它以确保生成正确的名称。

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)

请参阅动态模块未定义 init 函数 (PyInit_fuzzy)以及如何在 Cython 的 setup.py 中指定 Python 3 源代码?稍微详细一点(它与这些问题的重复接近,但在我看来并不完全)

回答by Den-Jason

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

我经历了这个,发现我必须使用与模块名称相同的 .pyx 名称,例如

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

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

客户端模块,安装到 venv 后运行

import myproj as myCppProjWrapper
...

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

我还发现,如果“myproj”名称不同,<python-lib-dir>/<python-vers>/site-packages则 .so 和 .egg-info 下的名称不同,客户端无法加载它。

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

另外我发现客户端的环境不需要cython安装这个包。