在 Raspberry Pi 上安装 Python 3 标准库

Install Python 3 Standard Library on Raspberry Pi

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

The Python installers for the Windows platform usually includes the entire standard library and often also include many additional components. For Unix-like operating systems Python is normally provided as a collection of packages, so it may be necessary to use the packaging tools provided with the operating system to obtain some or all of the optional components.

我已经尝试通过 apt-get 搜索图书馆,但结果是空的。我还安装了 pip,我想我可能会通过那条路线找到图书馆。没有库我无法编译我的代码。有人可以指出 where/how 我可以访问该库,以便我可以将 Python 脚本嵌入到我的 C 代码中吗?

如果您使用给定 in the python docs and save it in a file like pythonInC.c, you have to do two things (vartecs answer) 的示例代码(忘记使用 python3)。首先是示例代码:

#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. below)

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, however I did not solve that yet to give here as an easy answer. But there 可能是适合您的解决方案。