【问题标题】:cython works fine on winpython but not on anaconda - linking issuecython 在 winpython 上运行良好,但在 anaconda 上运行良好 - 链接问题
【发布时间】:2015-07-23 20:46:26
【问题描述】:

我有一个 Cython 代码,可以在 WinPython 下正常运行,但是当我从 WinPython 切换到 Anaconda3 时出现问题。我正在使用 Python 3.4

测试代码是:

# cython: boundscheck=False
# cython: wraparound=False
# cython: cdivision=True

cimport cython
cimport numpy as np
import numpy as np
from numpy cimport ndarray as ar
from libc.math cimport *


cpdef ar[double, ndim=1, mode='c'] test(ar[double, ndim=1, mode='c'] x):
    cdef:
        int n = x.shape[0]
        Py_ssize_t i
        ar[double, ndim=1, mode='c'] y = np.zeros(n)*np.nan
    with nogil:
        for i in range(0, n):
            y[i] = x[i]+1
    return y

编译代码为:

from distutils.core import setup
from distutils.extension import Extension
from Cython.Distutils import build_ext
import numpy as np

ext_modules = [Extension('test', ['tech/test.pyx'], include_dirs=[np.get_include()],
                         define_macros=[('NPY_NO_DEPRECATED_API', None)],
                         extra_compile_args=['-O3', '-march=native', '-ffast-math'],
                         libraries=['m']
                         )]

setup(
    name="Test Function",
    cmdclass={'build_ext': build_ext},
    ext_modules=ext_modules
)

我正在使用 MinGw 编译,所以在...\Anaconda3\Lib\distutils 文件夹下我有一个文件有

[build]
compiler = mingw32

另外在PATH环境变量中,我还有:

默认值: ...\蟒蛇3 ...\Anaconda3\脚本

还补充说: ...\Anaconda3\libs(这包含 python34)

还添加了:从 WinPython 复制的 mingw32 文件,其中包含 gcc 等: ...\Anaconda3\Tools\tools\mingw32\bin (这包含 gcc) ...\Anaconda3\Tools\tools\mingw32\x86_64-w64-mingw32\bin

当我尝试时:

python setup.py build_ext --inplace

代码运行良好,直到生成 test.ctest.otest.def。此后这就是我得到的:

C:\Anaconda3\Tools\tools\mingw32\bin\gcc.exe -shared -s build\temp.win-amd64-3.4\Release\tech\test.o build\temp.win-amd64-3.4\Release\tech\test.def -LC:\Anaconda3\libs -LC:\Anaconda3\PCbuild\amd64 -lm -lpython34 -lmsvcr100 -

o P:\Documents\Temp\python-master\python-master\common\test.pyd

build\temp.win-amd64-3.4\Release\tech\test.o:test.c:(.text+0x8e): undefined reference to `__imp_PyExc_TypeError'

build\temp.win-amd64-3.4\Release\tech\test.o:test.c:(.text+0x10e): undefined reference to `__imp_PyExc_ValueError'

build\temp.win-amd64-3.4\Release\tech\test.o:test.c:(.text+0x259): undefined reference to `__imp__PyThreadState_Current'
....
....
C:/Anaconda3/Tools/tools/mingw32/bin/../lib/gcc/x86_64-w64-mingw32/4.9.2/../../../../x86_64-w64-mingw3                ild\temp.win-amd64-3.4\Release\tech\test.o: bad reloc address 0x0 in section `.data'
collect2.exe: error: ld returned 1 exit status                                                                        
error: command 'C:\\Anaconda3\\Tools\\tools\\mingw32\\bin\\gcc.exe' failed with exit status 1   
  1. 首先,我在任何地方都找不到Anacond3\PCBuild\amd64 目录/文件。

  2. 我试图在整个互联网上查找,但找不到任何对 __imp_PyExc_TypeError__imp__PyThreadState_Current__imp_PyExc_ValueError 的引用。

最终转换为*.pyd 文件可能会出现什么问题?

【问题讨论】:

    标签: cython anaconda


    【解决方案1】:

    我怀疑 Anaconda 还不支持 mingw-64。

    在 Cark Kleffner 推出静态版本之前,从历史上看,mingw-64 确实是一个绝望的想法。

    当 Carl 的更新版本可用作 mingwpy 轮时,情况可能(应该?)发生变化。

    【讨论】:

      【解决方案2】:

      你可以运行

      conda remove libpython
      

      让 Anaconda 使用 Visual Studio 而不是 mingw,这可能会更好。

      【讨论】:

      • 其实我不想用Visual Studio :-(,偏爱远离MS
      • Visual Studio 在这类事情上的问题往往最少。如果你想远离 MS,你不应该使用 Windows。
      • 我正在远离 Windows,主要在 Linux(以及 Mac)上工作,但我仍然尝试确保代码也能在 Windows 上运行 - 主要是出于历史原因(而且我有一台功能强大的 Windows 机器,每个月都在变慢)
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-09-25
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多