Python——通过模块将C++类暴露给Cython

kq4fsx7k  于 2021-08-25  发布在  Java
关注(0)|答案(1)|浏览(595)

我想做什么:安装一个Python/Cython模块,公开C++类并能够 cimport 在任何情况下都是如此 .pyx 以后再归档。
什么不起作用:我不能设法去做 cimport 安装模块后,该文件将被删除。cython编译工作正常,因为我可以在纯python中使用 Package 类。
文件结构:

├── cytest
│   ├── cywrappers
│   │   ├── cynode.pxd
│   │   └── cynode.pyx
│   └── hpp
│       └── node.hpp
└── setup.py

node.hpp:


# ifndef graph_HPP

# define graph_HPP

class Node
{
public:
    int ID;
    double value;
    Node(){;};
    Node(int tid, double tvalue)
    {this->ID = tid; this->value = tvalue;}
    void multiplicate(double num){this->value = this->value * num;}
};

# endif

cynode.pxd

cdef extern from "node.hpp":
    cdef cppclass Node:
        Node()
        Node(int tid, double tvalue)
        int ID
        double value
        void multiplicate(double num)

cynode.pyx

cimport cynode
cdef class pynode:
    cdef cynode.Node c_node
    def __cinit__(self, int tid, double tval):
        self.c_node = cynode.Node(tid,tval)
    def print_val(self):
        print("ID: ", self.c_node.ID, "value: ", self.c_node.value)
    def multiplicate(self, mul):
        self.c_node.multiplicate(mul)

setup.py :

from setuptools import setup, find_packages, Extension
from Cython.Distutils import build_ext
import numpy as np
setup(
    name = "pycytest",
    packages=find_packages(include=['cytest', 'cytest.*']),
    package_data={'': ['*.pxd', '*.pyx', '*.hpp']},
    zip_safe=False,
    ext_modules=
        [Extension("cytest.cywrappers.cynode", 
         sources = ["cytest/cywrappers/cynode.pyx"], 
         language="c++", extra_compile_args=["-O3", "-std=c++11"],
         include_dirs=["./cytest/hpp/", np.get_include()])],
    cmdclass = {'build_ext': build_ext}
)

我安装了 pip install . 并试着在一个特定的环境中使用它 jupyter notebook (从另一个位置)。

import cytest.cywrappers.cynode as cynode
node = cynode.pynode(5, 7.6)
node.print_val()
node.multiplicate(67)
node.print_val()

应提供的产出:

('ID: ', 5, 'value: ', 7.6)
('ID: ', 5, 'value: ', 509.2)

但是,如果我尝试以下任何一行:

%%cython --cplus

# from cytest cimport cywrappers

# cimport cytest

# cimport cynode

我总是得到一份工作 'XXX.pxd' not found .
有人有办法吗?我搜索了相当长的一段时间,恐怕我没有找到正确的关键字。

cbwuti44

cbwuti441#

我设法让它工作起来:
我重新命名了扩展名以适应 pyx 我确保 pxd 输入 hpp 相对重要性( cdef extern from "../hpp/node.hpp": )
最后,为了使 package_data 查找并包含存储库中的所有文件(需要在以后重新使用代码 pyx ),我加了一个空的 __init__.py 在每个目录中。
现在我可以了 cimport cytest.cywrappers.cynode as cynode .
我尝试了很多东西,所以所有的编辑都不相关,但这里有一个工作 setup.py :

from setuptools import setup, find_packages, Extension
from Cython.Distutils import build_ext
from Cython.Build import cythonize
import numpy as np

extension = cythonize( [Extension("cytest.cywrappers.cynode", 
         sources = ["cytest/cywrappers/cynode.pyx"], 
         language="c++", extra_compile_args=["-O3", "-std=c++11"],
         include_dirs=["./cytest/hpp/", np.get_include()])], compiler_directives = {"language_level": 3, "embedsignature": True})

setup(
    name = "pycytest",
    packages=find_packages(include=['cytest', 'cytest.cywrappers']),
    package_data={"cytest": ["./*/*.pyx", "./*/*.pxd", "./*/*.hpp" ]},
    zip_safe=False,
    ext_modules=extension
       ,
    cmdclass = {'build_ext': build_ext}

)

相关问题