Error compiling Cython file: pxd not found in pack

2020-07-14 10:13发布

Trying to cimport pxd definitions from other packages.

Simple example, a.pxd file:

cdef inline void a():
    print "a"

b.pyx file:

cimport a

def b():
    a.a()

Until here, everything is ok, and $ cython b.pyx works.

If i move a.pxd to a folder, e.g libs/, then I change b.pyx to:

from libs cimport a

def b():
    a.a()

and then I have the error:

$ cython b.pyx 

Error compiling Cython file:
------------------------------------------------------------
...
from libs cimport a
^
------------------------------------------------------------

b.pyx:1:0: 'a.pxd' not found

Error compiling Cython file:
------------------------------------------------------------
...
from libs cimport a
^
------------------------------------------------------------

b.pyx:1:0: 'libs/a.pxd' not found

But libs/a.pxd is there. What would be the right way to import pxd definitions from other packages?

标签: cython
1条回答
狗以群分
2楼-- · 2020-07-14 10:36

A directory is not a package unless it contains a __init__.py file, even if the file is empty. So add an empty __init__.py file to the libs directory.


With this directory structure, your a.pxd and b.pyx, setup.py and script.py (below),

% tree .
.
├── libs
│   ├── a.pxd
│   └── __init__.py
├── b.c
├── b.pyx
├── b.so
├── build
│   ├── temp.linux-x86_64-2.7
│   │   └── b.o
│   └── temp.linux-x86_64-3.4
│       └── b.o
├── script.py
├── setup.py

Running script.py works:

% python setup.py build_ext --inplace
% python ./script.py 
a

setup.py:

# python setup.py build_ext --inplace

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

setup(
    name='test',
    ext_modules=cythonize("b.pyx"),
)

script.py:

import b
b.b()
查看更多
登录 后发表回答