我正在尝试使用cmake构建python扩展.这是cmake列表:
cmake_minimum_required(VERSION 2.8)
PROJECT(drtile)
set(CMAKE_MODULE_PATH ${CMAKE_CURRENT_SOURCE_DIR})
find_package(Vigra REQUIRED)
find_package(Boost COMPONENTS python REQUIRED)
find_package(PythonLibs REQUIRED)
find_package(Numpy REQUIRED)
include_directories(
${VIGRA_INCLUDE_DIR}
${PYTHON_NUMPY_INCLUDE_DIR}
${Boost_INCLUDE_DIR}
${PYTHON_INCLUDE_DIRS}
${PYTHON_INCLUDE_PATH}
)
add_library(drtile SHARED drtile.cpp)
message("xxx ${Boost_PYTHON_LIBRARY} ${VIGRA_NUMPY_CORE_LIBRARY}${VIGRA_NUMPY_IMPEX_LIBRARY}")
target_link_libraries(drtile ${Boost_PYTHON_LIBRARY} ${VIGRA_NUMPY_CORE_LIBRARY} ${PYTHON_LIBRARY})
IF(WIN32)
SET_TARGET_PROPERTIES(drtile PROPERTIES OUTPUT_NAME "drtile" PREFIX "" SUFFIX ".pyd")
ELSE()
SET_TARGET_PROPERTIES(drtile PROPERTIES OUTPUT_NAME "drtile" PREFIX "" SUFFIX ".so")
ENDIF()
Run Code Online (Sandbox Code Playgroud)
该库已正确编译和链接,但当我用otool查看喜欢的库时,我得到:
otool -L drtile.so
drtile.so:
/Users/lfiaschi/phd/workspace/lazyflow/lazyflow/drtile/drtile.so (compatibility version 0.0.0, current version 0.0.0)
libboost_python.dylib (compatibility version 0.0.0, current version 0.0.0)
vigranumpycore.so (compatibility version 0.0.0, current version 0.0.0)
/Users/lfiaschi/Library/Frameworks/Python.framework/Versions/2.7/Python (compatibility …Run Code Online (Sandbox Code Playgroud) 我有用C编写的Python扩展模块.我想在这个C代码中使用标准Python模块之一,例如os或shutil.怎么做到这一点?
为什么我只是无法构建sqlite?
它与readline或_tkinter或其他东西有关吗?
pes/libffi/src/prep_cif.o build/temp.linux-i686-2.7/home/mirror/build/tmp/Python-2.7.3/Modules/_ctypes/libffi/src/closures.o build/temp.linux-i686-2.7/home/mirror/build/tmp/Python-2.7.3/Modules/_ctypes/libffi/src/dlmalloc.o build/temp.linux-i686-2.7/home/mirror/build/tmp/Python-2.7.3/Modules/_ctypes/libffi/src/x86/ffi.o build/temp.linux-i686-2.7/home/mirror/build/tmp/Python-2.7.3/Modules/_ctypes/libffi/src/x86/sysv.o -L/usr/local/lib -o build/lib.linux-i686-2.7/_ctypes.so
Python build finished, but the necessary bits to build these modules were not found:
_bsddb _tkinter bsddb185
bz2 dbm gdbm
readline sunaudiodev
To find the necessary bits, look in setup.py in detect_modules() for the module's name.
Run Code Online (Sandbox Code Playgroud)
无法构建这些模块:
_sqlite3
running build_scripts
creating build/scripts-2.7
copying and adjusting /home/mirror/build/tmp/Python-2.7.3/Tools/scripts/pydoc -> build/scripts-2.7
copying and adjusting /home/mirror/build/tmp/Python-2.7.3/Tools/scripts/idle -> build/scripts-2.7
copying and adjusting /home/mirror/build/tmp/Python-2.7.3/Tools/scripts/2to3 -> build/scripts-2.7
copying and adjusting /home/mirror/build/tmp/Python-2.7.3/Lib/smtpd.py -> build/scripts-2.7
changing mode of build/scripts-2.7/pydoc …Run Code Online (Sandbox Code Playgroud) 我在 python 2.7.5 的帮助下找到了这个代码片段,这是一个关于将 C-API 暴露给其他模块的章节,在用 C 和 C++ 扩展 Python 部分:为扩展模块提供 C API
/* C API functions */
#define PySpam_System_NUM 0
#define PySpam_System_RETURN int
#define PySpam_System_PROTO (const char *command)
// ...
static PySpam_System_RETURN PySpam_System PySpam_System_PROTO;
// ...
static void **PySpam_API;
#define PySpam_System \
(*(PySpam_System_RETURN (*)PySpam_System_PROTO) PySpam_API[PySpam_System_NUM])
Run Code Online (Sandbox Code Playgroud)
这段代码是针对函数胶囊的。胶囊用于在两个模块之间传递函数。但是这个片段的含义是什么:[...] (PySpam_SystemRETURN (*)PySpam_System_PROTO) [...]. 我认为这有点像静态演员。类似的东西(int(*)(char s))。但是这个结构的意义是什么?
现在我有一个图像处理算法,在 Python 中大约有 100 行左右。使用numpy,PIL和大约需要 500 毫秒scipy。我希望让它更快,并且由于实际算法到目前为止似乎已经非常优化,我想知道是否使用不同的方法,例如Cython会改善时间。我相信我可以做几件不同的事情:
numpy这种方式。numpy或那些模块,但仍然非常有效。我只是在这里寻找速度,而不是担心实施的难度。在这种情况下,是否有更好的选择,它们都是一样的,还是值得做?
在遵循针对Python的C扩展教程时,我的模块似乎缺少其内容。虽然构建和导入模块没有问题,但是在模块中使用该功能失败。我在macOS上使用Python 3.7。
testmodule.c
#define PY_SSIZE_T_CLEAN
#include <Python.h>
static PyObject* add(PyObject *self, PyObject *args) {
const long long x, y;
if (!PyArg_ParseTuple(args, "LL", &x, &y)) {
return NULL;
}
return PyLong_FromLongLong(x + y);
}
static PyMethodDef TestMethods[] = {
{"add", add, METH_VARARGS, "Add two numbers."},
{NULL, NULL, 0, NULL}
};
static struct PyModuleDef testmodule = {
PyModuleDef_HEAD_INIT,
"test",
NULL,
-1,
TestMethods
};
PyMODINIT_FUNC PyInit_test(void)
{
return PyModule_Create(&testmodule);
}
Run Code Online (Sandbox Code Playgroud)
setup.py
from distutils.core import setup, Extension
module1 = Extension('test', sources=['testmodule.c'])
setup(name='Test',
version='1.0',
description='Test …Run Code Online (Sandbox Code Playgroud) 简而言之
我尝试编译一个名为extension2cimportsextension从自创建包中导入文件的 cython 扩展。构建时extension2,我收到了extension.pxd未找到的错误,尽管此文件正好位于指定的路径中。
细节
我正在构建两个涉及 cython 的包,一个包A和一个B依赖于A. A是命名空间包的子包nsp。也就是说,文件夹结构如下所示:
??? nsp
? ??? A
| ??? extension.pxd
| ??? extension.pyx
? ??? __init__.py
??? setup.py
Run Code Online (Sandbox Code Playgroud)
在这里,setup.py内容如下:
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 …Run Code Online (Sandbox Code Playgroud) 我正在尝试创建和分发(使用 pip)一个 Python 包,其中包含 Python 代码和使用 Pybind11(使用 Visual Studio 2019)编译为 .pyd 文件的 C++ 代码。我还想包含.pyi 存根文件,用于 VScode 和其他编辑器。我找不到太多关于正确执行此操作的文档。
我希望能够像平常一样通过 pip 安装包,并from mymodule.mysubmodule import myfunc像普通的 Python 包一样编写等,包括使用我编写的 .pyi 文件自动完成、类型注释、VScode 智能感知等。
我的 C++ 代码位于多个 cpp 和头文件中。它使用一些标准库和一些外部库(例如boost)。它定义了一个模块和 2 个子模块。我希望能够在 Windows 和 Linux 上以及 x86 和 x64 上分发它。我目前的目标是 Python 3.9 和 c++17 标准。
我应该如何构建和分发这个包?我是否包含 c++ 源文件,并创建一个类似于Pybind11 示例的 setup.py ?如果是这样,我如何包含外部库?如何构建 .pyi 存根文件?这是否意味着尝试安装我的软件包的人也需要 C++ 编译器?
或者,我应该将我的 c++ 编译为每个平台和架构的 .pyd/.so 文件吗?如果是这样,有没有办法指定通过 pip 安装哪个?再说一次,我将如何构建 .pyi 存根?
我正在为 python 创建一个 C++ 扩展。parent它创建一个包含子模块的模块child。有child一个方法hello()。如果我把它称为
import parent
parent.child.hello()
> 'Hi, World!'
Run Code Online (Sandbox Code Playgroud)
如果我尝试导入我的函数,它会失败
import parent
from parent.child import hello
> Traceback (most recent call last):
> File "<stdin>", line 1, in <module>
> ModuleNotFoundError: No module named 'parent.child'; 'parent' is not a package
parent.child
> <module 'child'>
Run Code Online (Sandbox Code Playgroud)
这是我的代码 setup.py
from setuptools import Extension, setup
# Define the extension module
extension_mod = Extension('parent',
sources=['custom.cc'])
# Define the setup parameters
setup(name='parent',
version='1.0',
description='A C++ extension …Run Code Online (Sandbox Code Playgroud) 我foo在 Python 扩展模块中有一个函数,它应该向 Python 返回一个整数元组。这可以使用Py_BuildValue以下方法轻松完成:
static PyObject*
foo(PyObject* self, PyObject* args)
{
int a = 0;
int b = 0;
/* calculations and stuff */
PyObject* out = Py_BuildValue("(iii)", a, b, a+b);
Py_INCREF(out);
return out;
}
Run Code Online (Sandbox Code Playgroud)
而不是Py_BuildValue,我想使用PyTuple_Pack,它确保返回值确实是一个元组。
在Python的C API文档说,PyTuple_Pack(3, a, b, a+b)相当于Py_BuildValue("(iii)", a, b, a+b)。这两个函数都返回一个新的类型引用PyPbject*。
因此,鉴于上面的代码,
static PyObject*
foo(PyObject* self, PyObject* args)
{
/* ... */
PyObject* out = PyTuple_Pack(3, a, b, a+b);
Py_INCREF(out); …Run Code Online (Sandbox Code Playgroud)