简体   繁体   English

Cython/Python 比较无效的语法错误

[英]Cython/Python comparison invalid syntax error

I would like to compare Python with Cython in term of time execution, so I wrote two files:我想在时间执行方面将 Python 与 Cython 进行比较,所以我写了两个文件:

fac.py文件

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

fastfac.pyx fastfac.pyx

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

Then I wrote a setup file:然后我写了一个安装文件:

setup.py安装程序.py

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

From the Powershell I executed the two commands:从 Powershell 我执行了两个命令:

pip install Cython
python setup.py build_ext --inplace

From the second command I get the following message:从第二个命令我收到以下消息:

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

However I tried to make a comparison, so I wrote the file:但是我试图进行比较,所以我写了文件:

comparison.py比较.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))

When I run it, I get this error message:当我运行它时,我收到以下错误消息:

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

It seems that in the file python.pyx the definition cpdef long fastfactorial(long n) is not recognized as a regular function definition but as a syntax error;似乎在文件python.pyx中,定义cpdef long fastfactorial(long n)未被识别为常规 function 定义,而是作为语法错误; in fact, if I try to run that file I get the error message:事实上,如果我尝试运行该文件,我会收到错误消息:

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

How can I solve?我该如何解决? How can I correctly define a cpdef inside a.pyx file?如何在 a.pyx 文件中正确定义cpdef what am I missing?我错过了什么?

The problem is not your definition of fastfactorial, it is the fact that your setup.py exited with an error and, presumably, without compiling fastfac into a c library.问题不在于您对 fastfactorial 的定义,而是您的 setup.py 退出并出现错误,并且可能没有将 fastfac 编译到 c 库中。 In general, you should always fix such errors.通常,您应该始终修复此类错误。

Your error appears to be happening because you don't have a Microsoft Visual C++ compiler installed.您的错误似乎正在发生,因为您没有安装 Microsoft Visual C++ 编译器。 You can follow the instructions in this answer to choose a version of Visual C++ to install.您可以按照答案中的说明选择要安装的 Visual C++ 版本。

You also have a warning about the language_level not being set.您还会收到有关未设置 language_level 的警告。 You shouldn't ignore warnings either so it is worth explicitly stating the level in your setup.py.您也不应该忽略警告,因此值得在 setup.py 中明确说明级别。

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM