0

I'm trying to write some C code on my Raspberry Pi (running Raspbian) that will call a Python script. The Python docs I've found indicate that I need to include the Standard Library in my C code via one of its headers (Python.h). The library is needed to call the Python interpreter from the C code. The docs say...

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.

I've tried searching for the library via apt-get, but, have come up empty. I've also installed pip figuring I might find the library via that route. I can't compile my code without the library. Can someone point me to where/how I can get access to the library so that I can embed a Python script into my C code?

rrirower
  • 4,338
  • 4
  • 27
  • 45

1 Answers1

1

If you use the example code (and forget about using python3) given in the python docs and save it in a file like pythonInC.c, you have to do two things (vartecs answer). First the example code:

#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;
}

2nd, the configuration steps (see below for dev3.x packages):

sudo apt-get install python-dev

and add (you have to use python3.x whenever python2.7 is written here (see below))

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

to your gcc command. When I did (again "python3.x" and 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

on my raspberry running raspbian, I got the expected output as you can see.

However, coming back to python3. I have 3.2 here an did the steps analogue to the given above with 2.7 changed to 3.2. This gave me an error:

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

There is at least one discussion here, however I did not solve that yet to give here as an easy answer. But there might be solutions for you.

Community
  • 1
  • 1
Stefan Bollmann
  • 640
  • 4
  • 12
  • 32
  • Thanks! Your answer helped solve my problem. Since I am using Geany on the Pi, I was able to set the compile and build configuration to point to the appropriate paths. I am now able to compile my code. – rrirower Jan 25 '15 at 19:02