如何从python的父文件夹中导入依赖的cython模块[英] How to import depending cython modules from parent folder in python

本文是小编为大家收集整理的关于如何从python的父文件夹中导入依赖的cython模块的处理方法,想解了如何从python的父文件夹中导入依赖的cython模块的问题怎么解决?如何从python的父文件夹中导入依赖的cython模块问题的解决办法?那么可以参考本文帮助大家快速定位并解决问题。

问题描述

想象以下文件夹结构:

/project
  run.py
  /Helper
    Helper.pxd
    Helper.pyx
    setup.py
  /Settings
    Settings.pxd
    Settings.pyx
    setup.py

助手使用设置中定义的类型设置和pysettings.pxd.因此,在助手中,我正在做:

from Settings cimport Settings, PySettings

在辅助目录中的设置中,我放置

include_path=[".", "../Settings/"]

在Cythonize命令中.因此,助手知道设置和所有内容.

在run.py中,我想导入设置和助手.设置导入正常,但是在执行

import Helper.Helper

我收到以下错误:

Traceback (most recent call last):
  File "run.py", line 1, in <module>
    import Helper.Helper
  File "../Settings/Settings.pxd", line 6, in init Helper
AttributeError: module 'Settings' has no attribute 'PySettings'

一旦一切都在同一目录中,此问题就会消失.

为了完整,您将在下面找到整个代码:

/project/run.py

import Settings.Settings as S
import Helper.Helper as H

settings = S.PySettings()
settings.doSomething()

H.PyMyFunction(settings)

/project/helper/helper.pxd

from Settings cimport Settings, PySettings

cdef extern from "../../cppCode/Helper/Helper.h":
  void myFunction(Settings settings)

/project/helper/helper.pyx

#distutils: sources = [../../cppCode/Helper/Helper.cpp, ../../cppCode/Settings/Settings.cpp]
#distutils: language = c++

def PyMyFunction(PySettings settings):
  myFunction(settings.c_settings)

/project/helper/setup.py

from distutils.core import setup
from Cython.Build import cythonize
from setuptools.extension import Extension

extensions = [
  Extension("Helper", ["Helper.pyx"])
]

setup(ext_modules=cythonize(extensions, include_path=[".", "../Settings/"]))

/project/settings/settings.pxd

cdef extern from "../../cppCode/Settings/Settings.h":
  cdef cppclass Settings:
    Settings() except +
    void doSomething()

cdef class PySettings:
  cdef Settings c_settings

/project/settings/settings.pyx

#distutils: sources = ../../cppCode/Settings/Settings.cpp
#distutils: language = c++

cdef class PySettings:
  def __cinit__(self):
    self.c_settings = Settings()

  def doSomething(self):
    self.c_settings.doSomething()

/project/settings/setup.py

from distutils.core import setup
from Cython.Build import cythonize
from setuptools.extension import Extension

extensions = [
  Extension("Settings", ["Settings.pyx"])
]

setup(ext_modules=cythonize(extensions))

推荐答案

" include_path"是为C所包含的.它对Python寻找PXD文件的位置没有影响.

您通常需要一个设置文件.更好的方法可能是:

setup.py
project/
  run.py
  __init__.pxd
  Settings/
    __init__.pxd
    Settings.pyx/pxd
  Helper/
    __init__.pxd
    Helper.pyx/pxd

这将创建一个称为"软件包"的单个模块,该模块将立即构建并立即安装.安装后,您的用户将可以执行from project import whatever.

您会注意到,我已经添加了一些 __init__.pxd文件.这些基本上的目的与__init__.py文件相同,除了它们将文件夹标识为Cython的Cimport机制的(sub)软件包.

助手.pxd然后开始:

from project.Settings.Settings cimport Settings, PySettings

我使用了完整的而不是相对导入,因为cimport和相对导入似乎有点不可靠.

setup.py如下:

from setuptools.extension import Extension
from setuptools import setup
from Cython.Build import cythonize

ext_modules = cythonize("project/*/*.pyx")

setup(ext_modules=ext_modules)

欢迎您明确使用Extension,但是对于简单的东西,似乎更容易将文件模式直接发送到Cythonize.


我只测试了此阶段的毒气阶段,因为我没有编译并运行它的C ++文件.

本文地址:https://www.itbaoku.cn/post/1793837.html