Search code examples
pythoncython

Cython No such file or directory: '.pyd' error on Windows


I'm trying to build the Hello World example from Cython tutorial. I have written both hello.pyx and setup.py files:

# hello.pyx
def say_hello_to(name):
    print("Hello %s!" % name)

# setup.py
try:
    from setuptools import setup
    from setuptools import Extension
except ImportError:
    from distutils.core import setup
    from distutils.extension import Extension

from Cython.Build import cythonize


setup(
  name='Hello world app',
  ext_modules=cythonize("hello.pyx"),
)

When I run

python setup.py build_ext --inplace

I get the following error:

copying build\lib.win-amd64-2.7\cython_test\hello.pyd -> cython_test
error: [Errno 2] No such file or directory: 'cython_test\\hello.pyd'

Build process works fine and I get a working hello.pyd file, but for some reason setup.py cannot copy the .pyd back into the working directory. How can I fix that?

hello.pyx and setup.py files are also available at BitBucket


Solution

  • I've solved this issue. It appeared that python setup.py command should be executed outside the project directory. The following code works fine.

    cd ..
    python setup.py build_ext --inplace
    

    UPDATE: a better way to solve the issue is to specify the package_dir option to setup function:

    setup(
        name='Hello world app',
        package_dir={'cython_test': ''},
        ext_modules=cythonize("hello.pyx"),
    )