首页 > 解决方案 > 发生异常:ModuleNotFoundError

问题描述

我目前正在尝试学习 Cython,并在此处遵循了教程。

我让它在 Ubuntu(在 vmware 上)中工作,但现在我正试图让它在 Windows 上工作(使用 VSCode)。我把我在 Ubuntu 中创建的所有文件都放到了我的机器上。

当我尝试运行我的 python 文件 testing.py 时,我收到此错误:发生异常:ModuleNotFoundError No module named 'example_cy'

这是 testing.py 的代码

import timeit

cy = timeit.timeit('example_cy.test(5000)', setup = 'import example_cy', number = 1000)
py = timeit.timeit('example_py.test(5000)', setup = 'import example_py', number = 1000)

print(cy, py)
print('Cython is {}x faster'.format(py/cy))

这是 example_cy.pyx 文件

cpdef int test(int x):
    cdef int y = 0
    cdef int i
    for i in range(x):
        y += i
    return y

example_py.py 文件

def test(x):
    y = 0
    for i in range(x):
        y += i
    return y

和 setup.py 代码

from distutils.core import setup
from Cython.Build import cythonize

setup(ext_modules = cythonize('example_cy.pyx'))

标签: pythoncpython-3.xcythoncythonize

解决方案


推荐阅读