示例cython代码似乎可以正确编译,但是;始终无法加载.pxd,如下所示:
%%cython
cimport cython
from libc.math cimport exp, sqrt, pow, log, erf
@cython.cdivision(True)
cdef double std_norm_cdf(double x) nogil:
return 0.5*(1+erf(x/sqrt(2.0)))
@cython.cdivision(True)
def black_scholes(double s, double k, double t, double v,
double rf, double div, double cp):
"""Price an option using the Black-Scholes model.
s : initial stock price
k : strike price
t : expiration time
v : volatility
rf : risk-free rate
div : dividend
cp : +1/-1 for call/put
"""
cdef double d1, d2, optprice
with nogil:
d1 = (log(s/k)+(rf-div+0.5*pow(v,2))*t)/(v*sqrt(t))
d2 = d1 - v*sqrt(t)
optprice = cp*s*exp(-div*t)*std_norm_cdf(cp*d1) - \
cp*k*exp(-rf*t)*std_norm_cdf(cp*d2)
return optprice
---------------------------------------------------------------------------
MemoryError Traceback (most recent call last)
<ipython-input-16-4d3d17a3a5b5> in <module>()
----> 1 get_ipython().run_cell_magic('cython', '', 'cimport cython\nfrom libc.math cimport exp, sqrt, pow, log, erf\n\n@cython.cdivision(True)\ncdef double std_norm_cdf(double x) nogil:\n return 0.5* (1+erf(x/sqrt(2.0)))\n\n@cython.cdivision(True)\ndef black_scholes(double s, double k, double t, double v,\n double rf, double div, double cp):\n """Price an option using the Black-Scholes model.\n \n s : initial stock price\n k : strike price\n t : expiration time\n v : volatility\n rf : risk-free rate\n div : dividend\n cp : +1/-1 for call/put\n """\n cdef double d1, d2, optprice\n with nogil:\n d1 = (log(s/k)+(rf-div+0.5*pow(v,2))*t)/(v*sqrt(t))\n d2 = d1 - v*sqrt(t)\n optprice = cp*s*exp(-div*t)*std_norm_cdf(cp*d1) - \\\n cp*k*exp(-rf*t)*std_norm_cdf(cp*d2)\n return optprice')
C:\Users\Mike\Anaconda3\lib\site-packages\IPython\core\interactiveshell.py in run_cell_magic(self, magic_name, line, cell)
2160 magic_arg_s = self.var_expand(line, stack_depth)
2161 with self.builtin_trap:
-> 2162 result = fn(magic_arg_s, cell)
2163 return result
2164
C:\Users\Mike\Anaconda3\lib\site-packages\IPython\extensions\cythonmagic.py in cython(self, line, cell)
C:\Users\Mike\Anaconda3\lib\site-packages\IPython\core\magic.py in <lambda>(f, *a, **k)
191 # but it's overkill for just that one bit of state.
192 def magic_deco(arg):
--> 193 call = lambda f, *a, **k: f(*a, **k)
194
195 if callable(arg):
C:\Users\Mike\Anaconda3\lib\site-packages\IPython\extensions\cythonmagic.py in cython(self, line, cell)
269 self._code_cache[key] = module_name
270
--> 271 module = imp.load_dynamic(module_name, module_path)
272 self._import_all(module)
273
MemoryError:
%debug
> c:\users\mike\anaconda3\lib\site- packages\ipython\extensions\cythonmagic.py(271)cython()
270
--> 271 module = imp.load_dynamic(module_name, module_path)
272 self._import_all(module)
ipdb> display module_name
display module_name: '_cython_magic_f60db5be6ea30a0e9aabaffef198b964'
ipdb> display module_path
display module_path: 'C:\\Users\\Mike\\.ipython\\cython\\_cython_magic_f60db5be6ea30a0e9aabaffef198b964.pyd'
在正确的目录中找到cython_magic_f60db&lt; ...&gt; .pyd。没有给出其他细节。它似乎不是系统内存耗尽。
感谢您的帮助。虽然我看到有几个人没有遇到类似的问题,但似乎没有人完全回答。
PS。使用具有python 2.7级别的Anaconda IPython运行相同的cython单元...在这种情况下,发生了相同的错误,但是;生成了以下添加错误消息...
ImportError Traceback (most recent call last)
<ipython-input-3-17bfa2bc0438> in <module>()
----> 1 get_ipython().run_cell_magic(u'cython', u'', u'cimport cython\nfrom libc.math cimport exp, sqrt, pow, log, erf\n\n@cython.cdivision(True)\ncdef double std_norm_cdf(double x) nogil:\n return 0.5* (1+erf(x/sqrt(2.0)))\n\n@cython.cdivision(True)\ndef black_scholes(double s, double k, double t, double v,\n double rf, double div, double cp):\n """Price an option using the Black-Scholes model.\n \n s : initial stock price\n k : strike price\n t : expiration time\n v : volatility\n rf : risk-free rate\n div : dividend\n cp : +1/-1 for call/put\n """\n cdef double d1, d2, optprice\n with nogil:\n d1 = (log(s/k)+(rf-div+0.5*pow(v,2))*t)/(v*sqrt(t))\n d2 = d1 - v*sqrt(t)\n optprice = cp*s*exp(-div*t)*std_norm_cdf(cp*d1) - \\\n cp*k*exp(- rf*t)*std_norm_cdf(cp*d2)\n return optprice')
C:\Users\Mike\Anaconda\lib\site-packages\IPython\core\interactiveshell.pyc in run_cell_magic(self, magic_name, line, cell)
2160 magic_arg_s = self.var_expand(line, stack_depth)
2161 with self.builtin_trap:
-> 2162 result = fn(magic_arg_s, cell)
2163 return result
2164
C:\Users\Mike\Anaconda\lib\site-packages\IPython\extensions\cythonmagic.py in cython(self, line, cell)
C:\Users\Mike\Anaconda\lib\site-packages\IPython\core\magic.pyc in <lambda>(f, *a, **k)
191 # but it's overkill for just that one bit of state.
192 def magic_deco(arg):
--> 193 call = lambda f, *a, **k: f(*a, **k)
194
195 if callable(arg):
C:\Users\Mike\Anaconda\lib\site-packages\IPython\extensions\cythonmagic.py in cython(self, line, cell)
269 self._code_cache[key] = module_name
270
--> 271 module = imp.load_dynamic(module_name, module_path)
272 self._import_all(module)
273
ImportError: DLL load failed: A dynamic link library (DLL) initialization routine failed.
上述故障与python 3.4完全相同,只是错误指示ImportError并指出DLL初始化例程有问题。
我检查了cython的源代码,但没有发现标准链接pragma指示dll main。
答案 0 :(得分:0)
将问题隔离到Anaconda的python包(2.7&amp; 3.4)中运行mingw c ++的已知错误之后。我找到了如何安装Microsoft C ++ for Python 2.7(MS免费)的说明,并使cython能够使用distutils包在Anconda(2.7测试版)中成功加载和运行更新。
请参阅网站链接https://github.com/cython/cython/wiki/CythonExtensionsOnWindows
中的“使用Microsoft Visual C ++编译器for Python”部分