Windows上的Cython内存视图 [英] Cython memoryviews on Windows
问题描述
当尝试在Windows上使用Cython(基于Anaconda的安装,因为需要TMP-GCC支持OpenMP)时,在使用键入的memoryviews时遇到错误。
When trying to use Cython on Windows (Anaconda-based install, using TDM-GCC as I need support for OpenMP), I ran into an error when using typed memoryviews.
test1.pyx
def test(int x): pass
test2.pyx
def test(int[:] x): pass
两个模块都可以使用基本setup.py进行编译(使用cythonize),但是可以毫无问题地导入test1,导入test2会引发以下内容:
Both modules can be compiled with a basic setup.py (using cythonize), but while test1 can be imported with no problem, importing test2 raises the following:
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行上没有什么特别的地方。
with nothing special at line 13146 of test.c, apparently.
这是一个已知问题吗?还是我做错了什么?
Is this a known issue? Or am I doing something wrong? Any help would be welcome.
(由Cython用户交叉张贴)
(crossposted from Cython-users)
说明:
- 同样,请注意,我使用的是Python 3(实际上,该错误并未出现在Python 2中)。 / li>
- 我正在使用Python 3.4.1和Cython 0.20.1在Conda环境中进行全新安装。
-
我正在使用
- Again, please note that I am using Python 3 (In fact, the bug doesn't appear with Python 2).
- I am using a clean install into a Conda environment, using Python 3.4.1 and Cython 0.20.1.
I am using the following setup.py.
来自distutils.core导入设置;从Cython.Build导入cythonize
setup(ext_modules = cythonize( test.pyx))
,但是更长的setup.py(如Saullo Castro建议的那样)也无济于事。
but a longer setup.py such as the one suggested by Saullo Castro doesn't help either.
赏金授予Saullo Castro,他指出即使我最终使用了其他解决方案,也不仅仅支持MinGW-64bit。
Bounty awarded to Saullo Castro for pointing out that MinGW-64bit is not simply supported, even though I ended up using a different solution.
推荐答案
我正在使用Windows 7 64位,Python 2.7.5 64位和Cython 0.20.1,您的代码对我有用。
I am using Windows 7 64-bit, Python 2.7.5 64 bit and Cython 0.20.1 and your code works for me.
我测试了您的原始代码,并且:
I tested your original code and this:
def test(int[:] x):
s = np.shape(x)[0]
for i in range(s):
print x[i]
没有问题。我将在这里描述如何由Cython进行编译以及如何配置C编译器以与Cython一起使用,以期您可以按照以下步骤解决您的问题。
without problems. I will describe here how I compiled by Cython and how I configured my C compiler to use with Cython with the hope that you can solve your problem following these steps.
-
下载并使用Microsoft SDK C编译器根据您的Python版本
在Windows中配置您的编译环境,对我来说是:
Configure your compiling environment in Windows, for me it is:
SET DISTUTILS_USE_SDK=1
setenv /x64 /release
-
编译Cython(只需执行
python setup.py
应该可以)为您的
.pyx
文件提供一个不错的setup.py
以下是我用来启用对 OpenMP 的支持的示例:
Have a nice setup.py
for your .pyx
files, here it follows a sample that I use to enable support to OpenMP:
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)
- 使用
import pyximport; pyximport.install()
(如果适用) - use
import pyximport; pyximport.install()
when applicable
这篇关于Windows上的Cython内存视图的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!