编译Cython文件时出错:在软件包中找不到pxd [英] Error compiling Cython file: pxd not found in package

查看:70
本文介绍了编译Cython文件时出错:在软件包中找不到pxd的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

尝试从其他软件包中导入pxd定义。

Trying to cimport pxd definitions from other packages.

简单示例a.pxd文件:

Simple example, a.pxd file:

cdef inline void a():
    print "a"

b.pyx文件:

cimport a

def b():
    a.a()

直到此处,一切正常,并且 $ cython b .pyx 有效。

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

如果我将a.pxd移至文件夹,例如 libs / ,然后将b.pyx更改为:

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()

然后出现错误:

$ 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

但是libs / a.pxd在那儿。
从其他软件包中导入pxd定义的正确方法是什么?

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

推荐答案

目录不是包,除非它包含 __ init __。py 文件,即使文件为空。因此,将一个空的 __ init __。py 文件添加到 libs 目录中。

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.

使用此目录结构,您的 a.pxd b.pyx setup.py script.py (如下),

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

运行 script.py 的工作原理:

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






setup.py:


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:


script.py:

import b
b.b()

这篇关于编译Cython文件时出错:在软件包中找不到pxd的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆