通过pip安装本地cython包并不能访问其模块 我试图用Cython矩形教程示例创建一个带有C++类Cython包装的简单包。 但是,当我使用pip安装包时,我无法在Python中导入它。以下是我在项目目录中的命令: python3.8 -m venv .venv source .venv/bin/activate pip install . python -c "from cython_cpp_example.cython.rect import PyRectangle"

通过pip安装本地cython包并不能访问其模块 我试图用Cython矩形教程示例创建一个带有C++类Cython包装的简单包。 但是,当我使用pip安装包时,我无法在Python中导入它。以下是我在项目目录中的命令: python3.8 -m venv .venv source .venv/bin/activate pip install . python -c "from cython_cpp_example.cython.rect import PyRectangle",python,python-3.x,pip,cython,Python,Python 3.x,Pip,Cython,最后一个命令失败,原因是: Traceback (most recent call last): File "<string>", line 1, in <module> ModuleNotFoundError: No module named 'cython_cpp_example.cython.rect' 当我使用python命令和--inplace参数构建包时,它也不会失败: pip install Cython python setup.

最后一个命令失败,原因是:

Traceback (most recent call last):
  File "<string>", line 1, in <module>
ModuleNotFoundError: No module named 'cython_cpp_example.cython.rect'
当我使用
python
命令和
--inplace
参数构建包时,它也不会失败:

pip install Cython
python setup.py build_ext --inplace
# this works:
python -c "from cython_cpp_example.cython.rect import PyRectangle"
还要注意的是,运行我的entrypoint
cython示例
,即使我通过
pip install安装程序包
,也可以工作。 我不想在原地安装软件包,也不想干预
sys.path
。我在我的项目中遗漏了什么,破坏了我的pip安装

该项目包含以下结构:

cython_cpp_example/
    cpp/
        Rectangle.cpp
        Rectangle.h
    cython/
        __init__.py
        rect.pyx
    __init__.py
    main.py
pyproject.toml
setup.py
Rectangle.cpp

#include "Rectangle.h"
using namespace shapes;
Rectangle::Rectangle(int X0, int Y0, int X1, int Y1) {x0 = X0; y0 = Y0; x1 = X1; y1 = Y1;}
Rectangle::~Rectangle() {}
int Rectangle::getLength() {return (x1 - x0);}
int Rectangle::getHeight() {return (y1 - y0);}
int Rectangle::getArea() {return (x1 - x0) * (y1 - y0);}
void Rectangle::move(int dx, int dy) {x0 += dx; y0 += dy; x1 += dx; y1 += dy;}
矩形.h

#ifndef RECTANGLE_H
#define RECTANGLE_H
namespace shapes {
class Rectangle {
public:
    int x0, y0, x1, y1;
    Rectangle(int x0, int y0, int x1, int y1);
    ~Rectangle();
    int getLength();
    int getHeight();
    int getArea();
    void move(int dx, int dy);
};
}
#endif
rect.pyx

cdef extern from "../cpp/Rectangle.cpp":
    pass

cdef extern from "../cpp/Rectangle.h" namespace "shapes":
    cdef cppclass Rectangle:
        Rectangle(int, int, int, int) except +
        int x0, y0, x1, y1
        int getLength()
        int getHeight()
        int getArea()
        void move(int, int)

cdef class PyRectangle:
    cdef Rectangle *thisptr
    def __cinit__(self, int x0, int y0, int x1, int y1):
        self.thisptr = new Rectangle(x0, y0, x1, y1)
    def __dealloc__(self):
        del self.thisptr
    def getLength(self):
        return self.thisptr.getLength()
    def getHeight(self):
        return self.thisptr.getHeight()
    def getArea(self):
        return self.thisptr.getArea()
    def move(self, dx, dy):
        self.thisptr.move(dx, dy)
main.py

from cython_cpp_example.cython.rect import PyRectangle

def main():
    print(PyRectangle(1, 2, 3, 4).getArea())

if __name__ == '__main__':
    main()
from setuptools import Extension, setup, find_packages
from Cython.Build import cythonize

setup(
    name='cython-cpp-example',
    version='0.2.0',
    entry_points={
        'console_scripts': [
            'cython-example = cython_cpp_example.main:main',
        ],
    },
    packages=find_packages(),
    ext_modules=cythonize([
        Extension("cython_cpp_example.cpp.Rectangle", sources=["cython_cpp_example/cpp/Rectangle.cpp"], language="c++"),
        Extension("cython_cpp_example.cython.rect", sources=["cython_cpp_example/cython/rect.pyx"], language="c++")
    ])
)
pyproject.toml

[build-system]
requires = ["setuptools", "wheel", "Cython"]
setup.py

from cython_cpp_example.cython.rect import PyRectangle

def main():
    print(PyRectangle(1, 2, 3, 4).getArea())

if __name__ == '__main__':
    main()
from setuptools import Extension, setup, find_packages
from Cython.Build import cythonize

setup(
    name='cython-cpp-example',
    version='0.2.0',
    entry_points={
        'console_scripts': [
            'cython-example = cython_cpp_example.main:main',
        ],
    },
    packages=find_packages(),
    ext_modules=cythonize([
        Extension("cython_cpp_example.cpp.Rectangle", sources=["cython_cpp_example/cpp/Rectangle.cpp"], language="c++"),
        Extension("cython_cpp_example.cython.rect", sources=["cython_cpp_example/cython/rect.pyx"], language="c++")
    ])
)