python - 带有 GSL 的 Windows 64 中的 Cython

标签 python c++ cython anaconda gsl

我正在尝试编译以下 pyx 代码:

#declaring external GSL functions to be used
cdef extern from "math.h":
    double sqrt(double)

cdef double Sqrt(double n):
   return sqrt(n)

cdef extern from "gsl/gsl_rng.h":
   ctypedef struct gsl_rng_type:
       pass
   ctypedef struct gsl_rng:
       pass
   gsl_rng_type *gsl_rng_mt19937
   gsl_rng *gsl_rng_alloc(gsl_rng_type * T)

cdef gsl_rng *r = gsl_rng_alloc(gsl_rng_mt19937)

cdef extern from "gsl/gsl_randist.h":
   double gamma "gsl_ran_gamma"(gsl_rng * r,double,double)
   double gaussian "gsl_ran_gaussian"(gsl_rng * r,double)

# original Cython code
def gibbs(int N=20000,int thin=500):
   cdef double x=0
   cdef double y=0
   cdef int i, j
   samples = []
   #print "Iter  x  y"
   for i in range(N):
       for j in range(thin):
           x = gamma(r,3,1.0/(y*y+4))
           y = gaussian(r,1.0/Sqrt(x+1))
       samples.append([x,y])
   return samples

smp = gibbs()

这是我的 setup.py 文件的样子:

from distutils.core import setup
from distutils.extension import Extension
from Cython.Distutils import build_ext

import numpy
import sys

if sys.platform == "win32":
    include_gsl_dir = sys.exec_prefix+"\gsl\include"
    lib_gsl_dir = sys.exec_prefix+"\gsl\lib"
else:
    include_gsl_dir = sys.exec_prefix+"\include"
    lib_gsl_dir = sys.exec_prefix+"\lib"

ext = Extension("samplers", ["samplers.pyx"],
    include_dirs=[numpy.get_include(), 
                  include_gsl_dir],
    library_dirs=[lib_gsl_dir],
    libraries=["gsl","gslcblas","m"]
)
setup(ext_modules=[ext],
    cmdclass = {'build_ext': build_ext})

GSL 文件包含在 C:\Users\MyName\Anaconda\gsl\include\gsl、C:\Users\MyName\Anaconda\gsl\lib\和 C:\Users\MyName\Anaconda\gsl\bin .因此,为什么 setup.py 文件包含对 sys.exec_prefix 的引用以获取 python 可执行文件所在的主 Anaconda 文件夹。

我将 GSL 与 Cython 链接的方式有什么问题吗?当我运行时:

 python setup.py build_ext --inplace

创建了一个 PYD 文件,但是当我尝试从创建 pyd 的同一文件夹中的 python 环境导入采样器时,我得到了可怕的“ImportError:DLL 加载失败:找不到指定的模块”。

我认为它编译错误要么是因为它无法与 GSL 文件通信,要么是 pyx 代码有问题。我排除了后者,因为它成功编译了一个 pyd 文件而没有错误。

我也尝试过与 CythonGSL 模块链接,但是如果您查看 init.py,因为 win32 gsl 需要位于 c:\Program Files\GnuWin32\include 中,我更喜欢在 Anaconda 文件夹中有 gsl 库。尽管如此,将它们放在 c:\Program Files\GnuWin32\include 中仍然给我同样的错误 ImportError: DLL...

最佳答案

当前版本的 CythonGSL 使用环境变量 LIB_GSL 来识别 GSL 的位置。仅当 LIB_GSL 不存在时,它才使用硬编码的 c:\Program Files\GnuWin32\include。因此,只要设置了 LIB_GSL,就可以将 GSL 安装放在不同的位置。我认为在设置 LIB_GSL 时,您应该使用 / 而不是 \

因此您需要创建/修改两个环境变量才能使 CythonGSL 工作:LIB_GSLPATH。我认为您可以在批处理文件中使用 setlocal。例如,我认为您可以执行以下操作(我将 gsl 安装在 C:\lib64\gsl)来临时设置它:

setlocal
SET LIB_GSL=C:/lib64/gsl
SET PATH=%PATH%;C:\lib64\gsl\bin;
Python

希望对您有所帮助。

关于python - 带有 GSL 的 Windows 64 中的 Cython,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27078594/

相关文章:

python - python 变量和函数的 vim 语法突出显示?

python - 安装 Tensorflow 警告 : Value for scheme. 脚本不匹配

c++ - BOOST_FUSION_ADAPT_STRUCT 没有采用正确数量的参数

c++ - 初始化列表中的空大括号魔术

c++ - OpenGL : glRotatef not working?

python - Cython:在类型声明中使用导入的类

python - Numba 与 Cython 循环优化

python - 用于运行脚本的 WxPython 按钮

python - 将时间增量应用于 csv 列

python - 如何将 Cython 数组转换为 Python 对象以便返回结果?