Cython/Python 比较无效的语法错误
Cython/Python comparison invalid syntax error
我想在时间执行方面比较Python和Cython,所以我写了两个文件:
fac.py
def factorial(n):
if n >= 1:
return n*factorial(n - 1)
return 1
快速fac.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
?
我错过了什么?
问题不在于您对 fastfactorial 的定义,而是您的 setup.py 退出时出现错误,并且可能没有将 fastfac 编译到 c 库中。通常,您应该始终修复此类错误。
你的错误似乎是因为你没有安装 Microsoft Visual C++ 编译器。您可以按照 this 答案中的说明选择要安装的 Visual C++ 版本。
您还收到有关未设置 language_level 的警告。您也不应忽略警告,因此值得在 setup.py.
中明确说明级别
setup(ext_modules=cythonize('fastfac.pyx'), language_level=3)
我想在时间执行方面比较Python和Cython,所以我写了两个文件:
fac.py
def factorial(n):
if n >= 1:
return n*factorial(n - 1)
return 1
快速fac.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
?
我错过了什么?
问题不在于您对 fastfactorial 的定义,而是您的 setup.py 退出时出现错误,并且可能没有将 fastfac 编译到 c 库中。通常,您应该始终修复此类错误。
你的错误似乎是因为你没有安装 Microsoft Visual C++ 编译器。您可以按照 this 答案中的说明选择要安装的 Visual C++ 版本。
您还收到有关未设置 language_level 的警告。您也不应忽略警告,因此值得在 setup.py.
中明确说明级别setup(ext_modules=cythonize('fastfac.pyx'), language_level=3)