c - 在 Raspberry Pi 上安装 Python 3 标准库

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

我正在尝试在我的 Raspberry Pi(运行 Raspbian)上编写一些 C 代码来调用 Python 脚本。 Python docs我发现表明我需要通过其 header 之一 (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 ,认为我可以通过该路线找到该库。如果没有库,我无法编译我的代码。有人可以告诉我在哪里/如何访问该库,以便我可以将 Python 脚本嵌入到我的 C 代码中吗?

最佳答案

如果您使用给定 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可能是适合您的解决方案。

关于c - 在 Raspberry Pi 上安装 Python 3 标准库,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28128824/

相关文章:

c - 关于函数指针的声明

python - 如何将列表写入 CSV 文件?

opencv - 无法在树莓派4上使用opencv 4.1.0-openvino打开IP摄像机

Python PyGtk 虚拟键盘支持 at-spi

c - printf 中的段错误

c++ - 矩阵的行、列和对角线之和

c - 函数指针作为另一个函数的参数时发出警告

python - 强制脚本在 Python 3 中运行

python-3.x - 如何创建一个功能来帮助找到给定距离内的所有地铁站?

c++ - QextSerialPort (QIODevice) 的 readRead() 信号调用速度不够快