当尝试在 Windows 上使用 Cython 时(基于 Anaconda 的安装,使用 TDM-GCC,因为我需要 OpenMP 支持),我在使用类型化内存视图时遇到了错误。
test1.pyx
def test(int x): pass
test2.pyx
def test(int[:] x): pass
这两个模块都可以使用基本的 setup.py (使用 cythonize)进行编译,但是虽然可以毫无问题地导入 test1,但导入 test2 会引发以下问题:
python3 -c "import test2" (<- Note the use of Python3 -- I haven't tried with Python2)
Traceback (most recent call last):
File "<string>", line 1, in <module>
File "stringsource", line 275, in init test2 (test2.c:13146)
UnicodeDecodeError: 'utf-8' codec can't decode byte in position 1: invalid start byte.
显然,test.c 的第 13146 行没有什么特别的。
这是一个已知的问题?或者我做错了什么?欢迎任何帮助。
(从 Cython 用户交叉发布)
澄清:
但是较长的 setup.py (如 Saullo Castro 建议的那样)也没有帮助。
Bounty授予 Saullo Castro,因为他指出 MinGW-64bit 并不受简单支持,尽管我最终使用了不同的解决方案。
我使用的是 Windows 7 64 位、Python 2.7.5 64 位和 Cython 0.20.1,您的代码适合我。
我测试了你的原始代码:
def test(int[:] x):
s = np.shape(x)[0]
for i in range(s):
print x[i]
没有什么问题。我将在这里描述我如何使用 Cython 进行编译以及如何配置我的 C 编译器以与 Cython 一起使用,希望您可以按照以下步骤解决您的问题。
SET DISTUTILS_USE_SDK=1
setenv /x64 /release
from distutils.core import setup
from distutils.extension import Extension
from Cython.Distutils import build_ext
ext_modules = [Extension('test1',
['test1.pyx'],
extra_compile_args=['/openmp', '/O2',
'/favor:INTEL64'])]
setup(name = 'test1',
cmdclass = {'build_ext': build_ext},
ext_modules = ext_modules)
- use
import pyximport; pyximport.install()
当适用
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)