【发布时间】:2014-11-22 14:41:34
【问题描述】:
我正在尝试编译以下 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 load failed: The specified module could not be found”。
我认为它编译错误是因为它无法与 GSL 文件通信,或者 pyx 代码有问题。我排除了后者,因为它成功编译了一个 pyd 文件而没有错误。
我也尝试与 CythonGSL 模块链接,但如果您查看 init.py,对于 win32,gsl 需要位于 c:\Program Files\GnuWin32\include 中,我更喜欢在 Anaconda 文件夹中有 gsl 库。尽管如此,将它们放在 c:\Program Files\GnuWin32\include 中仍然会给我同样的 ImportError: DLL 错误...
【问题讨论】:
-
如果您要在文件或文件夹路径中使用反斜杠,您需要对它们中的每一个进行反斜杠转义,或者使用 Python 原始字符串文字。例如
sys.exec_prefix+"\\gsl\\include"或sys.exec_prefix+r"\gsl\include"。使用os.path连接 (os.path.join()) 或以其他方式操作文件夹和文件路径也是一个好主意。
标签: python c++ cython anaconda gsl