【问题标题】:Cython/Python comparison invalid syntax errorCython/Python 比较无效的语法错误
【发布时间】:2020-09-16 17:29:45
【问题描述】:

我想比较一下 Python 和 Cython 的执行时间,所以我写了两个文件:

fac.py

def factorial(n):
    if n >= 1:
        return n*factorial(n - 1)
    return 1

fastfac.pyx

cpdef long fastfactorial(long n):
    if n>= 1:
        return n*fastfactorial(n - 1)
    return 1

然后我写了一个设置文件:

setup.py

from distutils.core import setup
from Cython.Build import cythonize
setup(ext_modules = cythonize('fastfac.pyx'))

我在 Powershell 中执行了两个命令:

pip install Cython
python setup.py build_ext --inplace

从第二个命令我得到以下消息:

Compiling fastfac.pyx because it changed.
[1/1] Cythonizing fastfac.pyx
C:\Users\.....\venv\lib\site-packages\Cython\Compiler\Main.py:369: FutureWarning: Cython directive 'language_level' not set, using 2 for now (Py2). This will change in a later release! File: C:\Users\.....\fastfac.pyx
  tree = Parsing.p_module(s, pxd, full_module_name)
running build_ext
building 'fastfac' extension
error: Unable to find vcvarsall.bat

但是我试图做一个比较,所以我写了文件:

comparison.py

from fastfac import fastfactorial
from fac import factorial
from timeit import timeit

print(timeit('fastfactorial(20)', globals = globals(), number = 10000))
print(timeit('factorial(20)', globals = globals(), number = 10000))

当我运行它时,我收到以下错误消息:

Traceback (most recent call last):
  File "C:/Users/...../comparison.py", line 1, in <module>
    from fastfac import fastfactorial
ModuleNotFoundError: No module named 'fastfac'

似乎在文件python.pyx 中定义cpdef long fastfactorial(long n) 没有被识别为常规函数定义,而是作为语法错误;事实上,如果我尝试运行该文件,我会收到错误消息:

  File "C:/Users/...../fastfac.pyx", line 1
    cpdef long fastfactorial(long n):
             ^
SyntaxError: invalid syntax

我该如何解决?如何在 .pyx 文件中正确定义 cpdef? 我错过了什么?

【问题讨论】:

  • Cythonizing fastfac.pyx,失败:error: Unable to find vcvarsall.bat,你应该先解决这个问题
  • 这能回答你的问题吗? error: Unable to find vcvarsall.bat
  • Duplicate 假设您安装了 Visual Studio。对你来说可能不是这样。然后你需要安装它。

标签: python syntax cython


【解决方案1】:

问题不在于您对 fastfactorial 的定义,而是您的 setup.py 退出并出现错误,并且可能没有将 fastfac 编译到 c 库中。通常,您应该始终修复此类错误。

您的错误似乎正在发生,因为您没有安装 Microsoft Visual C++ 编译器。您可以按照this答案中的说明选择要安装的Visual C++版本。

您还会收到有关未设置语言级别的警告。您也不应该忽略警告,因此值得在 setup.py 中明确说明级别。

setup(ext_modules=cythonize('fastfac.pyx'), language_level=3)

【讨论】:

  • 我按照您的指示修改了设置行,现在我收到错误:C:\Users\.....\Python37-32\lib\distutils\dist.py:274: UserWarning: Unknown distribution option: 'language_level' warnings.warn(msg) running build_ext building 'fastfac' extension error: Unable to find vcvarsall.bat
  • 我访问了您指定的链接并按照项目符号列表进行操作;在我正在运行的这台机器上:Python 3.7.0 (v3.7.0:1bf9cc5093, Jun 27 2018, 04:06:47) [MSC v.1914 32 bit (Intel)] on win32 所以我下载并安装了适当版本的 Visual Studio C++ 2017 (15.7)。然后我运行命令C:\Program Files (x86)\Microsoft Visual Studio 9.0\Common7\Tools\vsvars32.bat,我得到了C:\Program Files (x86)\Microsoft Visual Studio 9.0\Common7\Tools\vsvars32.bat的答案,没有任何进一步的信息。
  • 命令python setup.py build_ext --inplace 仍然抛出与以前相同的错误。
  • 显然,当您运行 vsvars.bat 时,您应该收到类似“设置使用 Microsoft Visual Studio 的环境...”的消息。你是从 cmd 提示符下运行它的吗?
  • 即使很难,我也安装了所需的 VC++ 版本 (link),在我的 Program Files (x86) 中没有 Microsoft Visual Studio 9.0 文件夹,既不是 vsvars32.bat 也不是 vsvars64.bat,如您所见这里link;所以 cmd 提示符无法运行找不到的文件
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2020-05-05
  • 1970-01-01
  • 2016-08-07
  • 2015-05-24
相关资源
最近更新 更多