python - Cython 导入错误

标签 python path cython importerror

以下文件为 helloworld.pyx:

print("Hello World")

以下文件为 setup.py:

from distutils.core import setup
from distutils.extension import Extension
from Cython.Distutils import build_ext
ext_modules = [Extension("helloworld",["helloworld.pyx"]
setup(
    name = 'HW',
    cmdclass = {'build_ext': build_ext},
    ext_modules = ext_modules
)

使用python setup.py build_ext --inplace后,我得到了我的*.so文件

因此,我将 *.so 重命名为 hw.so,以获得更短的导入名称。

但是如果我运行 python 并输入:import hw 我收到此错误:

ImportError: dynamic module does not define init function (PyInit_hw)

大约 3 小时前我正在做同样的事情,一切都很好。但我从这边尝试了一些东西:http://sourceforge.net/p/ubertooth/mailman/message/31699880/

我尝试了以下方法:

cmake -DPYTHON_EXECUTABLE=$(which python2) \
-DPYTHON_INCLUDE_DIR=$(echo /usr/include/python2*) \
-DPYTHON_LIBRARY=$(echo /usr/lib/libpython2.*.so) \

因为我想修复一些东西。我用“3”替换了所有“2”,因为我正在使用 python3.4

在我做了这个之后,我总是收到上面的错误。我破坏了任何路径吗?我怎样才能撤消它?谢谢你的帮助

最佳答案

当查看有关“使用 C 或 C++ 扩展 Python”的 Python 3 文档时,我们发现

The initialization function must be named PyInit_name(), where name is the name of the module, and should be the only non-static item defined in the module file.

( https://docs.python.org/3/extending/extending.html#the-module-s-method-table-and-initialization-function )

这意味着我们不能只更改模块的文件名而不更改 init 函数。我们必须首先使用最终名称来编译模块。编译后重命名.so文件将不起作用。

关于python - Cython 导入错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27207874/

相关文章:

python - 在 cython 函数上使用 timeit

python - 存储随机状态

python - 在python中从数组构造多行字符串

path - Julia:在相对于脚本位置的位置创建一个新文件夹和文件

java - IntelliJ 14 IDEA - 找不到文件的正确路径

cython - 为什么cpdef不与cython中的__init__一起使用

python - 变分自动编码器损失函数(keras)

python - (mach-o 文件,但它是一个不兼容的架构(有 'arm64' ,需要 'x86_64' ))

java - 正斜杠在 Path.resolve 中可以跨平台工作吗?

python - 如何编译整个 Python 库(包括依赖项)以便它可以在 C 中使用?