Tag: python

尝试构建基本的python扩展示例失败(windows)

我在Win7 x64机器上运行Python 2.6和Visual Studio 2008。 当我尝试在python 2.6源代码分发中找到的c“example_nt”中构建基本的python扩展示例时,它会失败: python setup.py build 这导致: running build running build_ext building ‘aspell’ extension Traceback (most recent call last): File “setup.py”, line 7, in ext_modules = [module1]) File “C:\Python26\lib\distutils\core.py”, line 152, in setup dist.run_commands() File “C:\Python26\lib\distutils\dist.py”, line 975, in run_commands self.run_command(cmd) File “C:\Python26\lib\distutils\dist.py”, line 995, in run_command cmd_obj.run() File “C:\Python26\lib\distutils\command\build.py”, line […]

Python扩展:找不到架构x86_64错误的符号

我想在c中写一个python扩展。 我在Mac上工作,我从这里拿了一个代码: #include static PyObject* say_hello(PyObject* self, PyObject* args) { const char* name; if (!PyArg_ParseTuple(args, “s”, &name)) return NULL; printf(“Hello %s!\n”, name); Py_RETURN_NONE; } static PyMethodDef HelloMethods[] = { {“say_hello”, say_hello, METH_VARARGS, “Greet somebody.”}, {NULL, NULL, 0, NULL} }; PyMODINIT_FUNC inithello(void) { (void) Py_InitModule(“hello”, HelloMethods); } 我编译它: gcc -c -o py_module.o py_module.c -I/Library/Frameworks/Python.framework/Versions/2.7/include/python2.7/ gcc -o […]

集成C和Python:ValueError:模块函数不能设置METH_CLASS或METH_STATIC

我正在第一次尝试集成C和Python 2.7.3。 首先,我只是想为Python编写一个可以进行基本添加的C模块。 (它被称为npfind,因为一旦我弄明白了,我想为numpy编写一个find方法) npfind.h: #include extern int add(int a, int b); npfind.c: #include “npfind.h” int add(int a, int b) { return a + b; } pynpfind.c: #include “Python.h” #include “npfind.h” static char* py_add_doc = “Adds two numbers.”; static PyObject* py_add(PyObject* self, PyObject* args) { int a, b, r; if (!PyArg_ParseTuple(args, “ii”, &a, &b)) { […]

表达式中的变量赋值

这是我的代码,用于在低于10,000,000的fibonnacci序列中生成值。 3 fibs = [1,1] 4 while((x = fibs[-1] + fibs[-2]) <= 10000000): 5 fibs.append(x) 我试图在while循环的条件下进行x样式的C赋值。 不幸的是,python告诉我这是一个语法错误。 什么是最简单的解决方案?

(ctypes)msvcrt.printf并在python中打印

我正在学习使用msvcrt.dll,python 2.7中的ctypes使用printf函数。 我观察到当使用msvcrt.printf(“x”)和通常的python print(“y”)函数时,“y”在“x”之前打印,而在“msvcret.printf”之后放置print(“y”)语句。 “X”) 。 任何人都可以解释为什么会这样吗? 这是我的代码: from ctypes import * msvcrt = cdll.msvcrt msvcrt.printf(“hello world!”) print(“abcd”) 输出: abcd hello world

为什么我不能在c中嵌入python时导入’math’库?

我正在使用python 2.6文档中的示例开始尝试在C中嵌入一些python。 示例C代码不允许我执行以下1行脚本: import math 使用线: ./tmp.exe tmp foo bar 它抱怨 Traceback (most recent call last): File “/home/rbroger1/scripts/tmp.py”, line 1, in import math ImportError: […]/python/2.6.2/lib/python2.6/lib-dynload/math.so: undefined symbol: PyInt_FromLong 当我在生成的二进制文件(tmp.exe)上执行nm ,它会显示 0000000000420d30 T PyInt_FromLong 该函数似乎已定义,为什么共享对象不能找到该函数?

Python C互操作性

我希望将现有的C(纯C,无.C ++)库包装到Python中,以便我可以从Python脚本中调用它。 各种可用的方法(C Api,SWIG等)最合适?

使用MinGW编译Cython – 未定义的引用PyExc

我正在尝试从“Cython – Python程序员指南”一书中编译一个简单的代码片段,当我编译时,我收到以下错误: H:\Cython>python setup.py build_ext -i –compiler=mingw32 running build_ext building ‘fib’ extension C:\MinGW\bin\gcc.exe -mdll -O -Wall -IC:\Anaconda3\include -IC:\Anaconda3\include -c fib.c -o build\temp.win32-3.4\Release\fib.o writing build\temp.win32-3.4\Release\fib.def C:\MinGW\bin\gcc.exe -shared -s build\temp.win32-3.4\Release\fib.o build\temp.win32-3.4\Release\fib.def -LC:\Anaconda3\libs -LC:\Ana conda3\PCbuild -lpython34 -lmsvcr100 -o H:\Cython\fib.pyd build\temp.win32-3.4\Release\fib.o:fib.c:(.text+0xb6): undefined reference to `_imp__PyExc_TypeError’ build\temp.win32-3.4\Release\fib.o:fib.c:(.text+0xf3): undefined reference to `_imp__PyExc_TypeError’ build\temp.win32-3.4\Release\fib.o:fib.c:(.text+0x3cc): undefined reference to `_imp___PyThreadState_Current’ build\temp.win32-3.4\Release\fib.o:fib.c:(.text+0x857): undefined reference […]

如何为python编译C / C ++函数

我有一个用cpp编写的源代码,我想编译并在Python中可读。 Python必须提供2个文件名作为输入,并检索矩阵和向量作为输出。 如果我必须在C / C ++中使用它,我会使用2个字符和2个指针,但我不知道如何在Python语言中看到编译的C / C ++程序。 我希望有人可以帮助我。 非常感谢你。

我们如何将C结构传递给Python?

我是C和Python的新手。 这些天,我正在学习在C中嵌入Python。当我从C调用Python函数时,我想知道,我们如何将C结构传递给Python? 非常感谢!