在Raspberry Pi上安装Python 3标准库

时间:2015-01-24 18:31:49

标签: c python-3.x raspberry-pi raspbian

我试图在我的Raspberry Pi(运行Raspbian)上编写一些C代码来调用Python脚本。我发现的Python docs表明我需要通过其标头之一(Python.h)将标准库包含在我的C代码中。需要从C代码调用Python解释器的库。文档说......

  

Windows平台的Python安装程序通常包括   整个标准库,通常还包括许多额外的   组件。对于类Unix 操作系统,Python通常是   作为包的集合提供,因此可能需要使用   提供操作系统的包装工具可以获得一些   或所有可选组件。

我已尝试通过 apt-get 搜索该库,但是,它已经空了。我也安装了pip figuring我可能会通过这条路线找到图书馆。我不能在没有库的情况下编译我的代码。有人能指出我在哪里/如何访问库,以便我可以将Python脚本嵌入到我的C代码中?

1 个答案:

答案 0 :(得分:1)

如果你使用给定in the python docs的示例代码(并忘记使用python3)并将其保存在pythonInC.c这样的文件中,则必须执行two things (vartecs answer)。首先是示例代码:

#include <Python.h>

int
main(int argc, char *argv[])
{
  Py_SetProgramName(argv[0]);  /* optional but recommended */
  Py_Initialize();
  PyRun_SimpleString("from time import time,ctime\n"
                     "print 'Today is',ctime(time())\n");
  Py_Finalize();
  return 0;
}

第2,配置步骤(见下面的dev3.x包):

sudo apt-get install python-dev

并添加(每当写入python2.7时都必须使用python3.x(见下文))

-I/usr/include/python2.7 -lpython2.7

到你的gcc命令。当我这样做时(再次“python3.x”和下面的s。)

user ~/stack $ gcc -I/usr/include/python2.7 -lpython2.7 pythonInC.c
user ~/stack $ ./a.out 
Today is Sun Jan 25 13:03:37 2015

在我的覆盆子上运行raspbian,我得到了预期的输出,你可以看到。

然而,回到python3。我有3.2在这里做了类似于上面给出的步骤,2.7改为3.2。这给了我一个错误:

user ~/programming/stack $ gcc -Wall -I/usr/include/python3.2 -lpython3.2 pythonInC.c 
pythonInC.c: In function 'main':
pythonInC.c:6:3: warning: passing argument 1 of 'Py_SetProgramName' from incompatible pointer type [enabled by default]
/usr/include/python3.2/pythonrun.h:25:18: note: expected 'wchar_t *' but argument is of type 'char *'
/usr/bin/ld: cannot find -lpython3.2
collect2: ld returned 1 exit status

至少有一次讨论here,但我还没有解决这个问题,只能作为一个简单的答案。但there可能是您的解决方案。