Cython Compilation Error: dynamic module does not define module export function
Cython Compilation Error: dynamic module does not define module export function
我正在用 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),
)
但是,我在 Python 3 中安装时遇到错误。它在 Python 2 中工作但是,它没有在 Python 3 中编译,出现以下错误:
dynamic module does not define module export function
我该如何解决这个问题? setup.py
的结构是不编译的原因吗?
您需要用 Python 3 调用 setup.py(python3 setup.py build_ext
,也许 --inplace
)。这是因为 Python 3 为模块启动时调用的 init
函数定义了不同的名称,因此您需要使用 Python 3 来构建它以确保生成正确的名称。
有关更多详细信息,请参阅 and How to specify Python 3 source in Cython's setup.py?(它与这些问题的重复接壤,但在我看来并不完全相同)
我经历过这个,发现我必须使用与模块名称相同的 .pyx 名称,例如
生成文件:
# (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))
客户端模块,运行 安装到 venv 后
import myproj as myCppProjWrapper
...
我还发现如果"myproj"名称不同,在<python-lib-dir>/<python-vers>/site-packages
下.so和.egg-info名称不同,客户端加载失败。
另外我发现客户端环境不需要安装cython
包
我对 torchvision
也有同样的错误。通过降级安装版本修复它:
pip install torch==1.2.0+cu92 torchvision==0.4.0+cu92 -f https://download.pytorch.org/whl/torch_stable.html
我正在用 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),
)
但是,我在 Python 3 中安装时遇到错误。它在 Python 2 中工作但是,它没有在 Python 3 中编译,出现以下错误:
dynamic module does not define module export function
我该如何解决这个问题? setup.py
的结构是不编译的原因吗?
您需要用 Python 3 调用 setup.py(python3 setup.py build_ext
,也许 --inplace
)。这是因为 Python 3 为模块启动时调用的 init
函数定义了不同的名称,因此您需要使用 Python 3 来构建它以确保生成正确的名称。
有关更多详细信息,请参阅
我经历过这个,发现我必须使用与模块名称相同的 .pyx 名称,例如
生成文件:
# (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))
客户端模块,运行 安装到 venv 后
import myproj as myCppProjWrapper
...
我还发现如果"myproj"名称不同,在<python-lib-dir>/<python-vers>/site-packages
下.so和.egg-info名称不同,客户端加载失败。
另外我发现客户端环境不需要安装cython
包
我对 torchvision
也有同样的错误。通过降级安装版本修复它:
pip install torch==1.2.0+cu92 torchvision==0.4.0+cu92 -f https://download.pytorch.org/whl/torch_stable.html