程序包的Cython C级接口:* .pxd文件未找到 [英] Cython C-level interface of package: *.pxd files are not found
问题描述
简而言之
我尝试编译一个名为 extension2
的cython扩展从自己创建的包中导入文件扩展名
。构建 extension2
时,出现错误:虽然此文件正好位于分隔路径中,但未找到 extension.pxd
。
详细信息
我正在构建两个涉及cython的软件包,一个软件包 A
和依赖于 A
的软件包 B
。 A
是命名空间包 nsp
的子包。也就是说,文件夹结构如下所示:
├──nsp
│└──A
| ├──extension.pxd
| ├──extension.pyx
│└──__init__.py
└──setup.py
在这里, setup.py
的内容如下:
从setuptools导入setup
从setuptools.extension import扩展
#工厂功能
def my_build_ext(pars):
#导入延迟:
从setuptools.command.build_ext将build_ext导入为_build_ext
#include_dirs已调整:
class build_ext(_build_ext):
def finalize_options(self):
_build_ext.finalize_options(self) )
#防止numpy认为它仍在设置过程中:
__builtins __.__ NUMPY_SETUP__ = False
import numpy
self.include_dirs.append(numpy.get_include())
#返回对象:
return build_ext(pars)
extensions = [Extension(nsp.A.extension,['nsp / A / extension.cpp'] )]
设置(
cmdclass = {'build_ext' :my_build_ext},
setup_requires = ['numpy'],
install_requires = ['numpy'],
个包= ['nsp.A'],
ext_modules =扩展
package_data = {
'nsp / A':['* .pxd','* .pyx']
},
)
设置文件的灵感来自>添加numpy-get-include-argument-to-setuptools-没有预安装的numpy 和 distributing-cython-modules 。 cython文件已使用另一个脚本成功转换为 .cpp
文件。
我使用
<$ p安装软件包
A
$ p> pip install。 setup.py
目录中的
。一切正常,我可以在 ... \Anaconda3\Lib\site-packages\nsp\A
下找到该包的所有文件,包括 *。pxd
文件。
现在,我尝试为扩展名2 $创建一个
*。cpp
文件c $ c>,以便稍后将其打包在第二个包 B
中。文件 extension2.pxd
从nsp.A.extension cimport读取
mymethod
用于创建 *。cpp
文件读取
从distutils.core导入设置,扩展名
从Cython。构建导入cythonize
导入numpy as np
import sys
print(sys.executable)
NAME ='extension2'
extensions = [Extension(NAME,[NAME +'。pyx'] ,
include_dirs = [np.get_include()]
)
]
设置(名称= NAME,
ext_modules = cythonize(扩展名,语言= c ++,
Compiler_directives = compiler_directives),
include_dirs = [np.get_include()]
)
当我使用 python myscript build_ext --inplace
运行此脚本时,出现错误,指出 pxd
文件丢失:
f rom nsp.A.extension cimport mymethod
^
--------------------------------- ---------------------------
.\extension2.pxd:11:0:'nsp\找不到A\extension.pxd'
但是,此文件完全存在。 ( sys.executable
是包含已安装软件包的 Anaconda3
文件夹)如何解决此问题?
其他信息
我正在Windows x64上使用python 3.7
Cython不支持隐式命名空间包。也就是说,cython仅搜索包含文件 init。*
的子目录,因此 *
可以是 py
, pyc
, pyx
和 pxd
。
我已经创建了 bugtracker报告,此问题,以防您想了解是否已在较新版本中解决此问题(我与Cython 0.29.14一起工作过。)。
在此之前,解决方法是在文件夹 nsp
中创建一个空文件 __ init __。pxd
。该文件应由python忽略,因为它不是 *。py
文件,并且可以让cython在子目录中搜索包。然后,文件结构如下:
├──nsp
│├──__init __。pxd
│└──A
| ├──extension.pxd
| ├──extension.pyx
│└──__init__.py
└──setup.py
要在名称空间包中安装其他文件 __ init __。pxd
,请更改 packages
参数 setup(...)
到 packages = ['nsp','nsp.A']
和<$ package_data = {''的c $ c> package_data
参数:['* .pxd','* .pyx']} 。
编辑:
该错误已为cython开发人员所知,并将得到修复在版本3中。请参见从PEP420名称空间修复cimport 。
In a nutshell
I try to compile a cython extension called extension2
that cimports a file extension
from a self-created package. When building extension2
, I get the error that extension.pxd
is not found though this file is exactly at the sepcified path.
Details
I am building two packages involving cython, a package A
and a package B
that depends on A
. A
is a subpacke of a namespace package nsp
. That is, the folder structure looks as follows:
├── nsp
│ └── A
| ├── extension.pxd
| ├── extension.pyx
│ └── __init__.py
└── setup.py
Here, setup.py
reads as follows:
from setuptools import setup
from setuptools.extension import Extension
# factory function
def my_build_ext(pars):
# import delayed:
from setuptools.command.build_ext import build_ext as _build_ext
# include_dirs adjusted:
class build_ext(_build_ext):
def finalize_options(self):
_build_ext.finalize_options(self)
# Prevent numpy from thinking it is still in its setup process:
__builtins__.__NUMPY_SETUP__ = False
import numpy
self.include_dirs.append(numpy.get_include())
#object returned:
return build_ext(pars)
extensions = [Extension(nsp.A.extension, ['nsp/A/extension.cpp'])]
setup(
cmdclass={'build_ext' : my_build_ext},
setup_requires=['numpy'],
install_requires=['numpy'],
packages=['nsp.A'],
ext_modules=extensions
package_data={
'nsp/A': ['*.pxd', '*.pyx']
},
)
The setup file is inspired by add-numpy-get-include-argument-to-setuptools-without-preinstalled-numpy and distributing-cython-modules. The cython files were already successfully transformed to .cpp
files with another script.
I install the the package A
with
pip install .
in the directory of the setup.py
. Everything works as desired, and I can find all files of the package under ...\Anaconda3\Lib\site-packages\nsp\A
, including the *.pxd
files.
Now I seek to create a *.cpp
file for an extension2
in order to package it later in the second package B
. The file extension2.pxd
reads
from nsp.A.extension cimport mymethod
The script to create the *.cpp
file reads
from distutils.core import setup, Extension
from Cython.Build import cythonize
import numpy as np
import sys
print(sys.executable)
NAME = 'extension2'
extensions = [Extension(NAME, [NAME+'.pyx'],
include_dirs=[np.get_include()]
)
]
setup(name=NAME,
ext_modules = cythonize(extensions, language="c++",
compiler_directives=compiler_directives),
include_dirs=[np.get_include()]
)
When I run this script with python myscript build_ext --inplace
, I get an error indicating the pxd
file is missing:
from nsp.A.extension cimport mymethod
^
------------------------------------------------------------
.\extension2.pxd:11:0: 'nsp\A\extension.pxd' not found
However, this file exists exactly there. (sys.executable
is the Anaconda3
folder that contains the installed package) How can I resolve the issue?
Additional info
I am using python 3.7 on Windows x64
Cython does not support implicit namespace packages as of yet. That is, cython searches only subdirectories that contain a file init.*
, whereby *
can be anything from py
, pyc
, pyx
, and pxd
.
I have created a bugtracker report for this issue, in case you want to follow up on whether the issue has been fixed in a newer version yet (I worked with Cython 0.29.14).
Until then, a workaround is to create an empty file __init__.pxd
in the folder nsp
. This file should be ignored by python, as it is not a *.py
file, and lets cython search the subdirectories for packages. The file structure then reads as follows:
├── nsp
│ ├── __init__.pxd
│ └── A
| ├── extension.pxd
| ├── extension.pyx
│ └── __init__.py
└── setup.py
To install the additional file __init__.pxd
in the namespace package, change the packages
argument of setup(...)
to packages=['nsp', 'nsp.A']
and the package_data
argument to package_data={'': ['*.pxd', '*.pyx']}
.
Edit:
The bug has been known to the cython developers and will be fixed in version 3. See Fix for cimport from PEP420 namespace.
这篇关于程序包的Cython C级接口:* .pxd文件未找到的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!