Lots of people have multiple times asked me below questions:
1. How to call binaries (or other programs) from a python script?
2. How to call python script from another program say a c code?
3. How to embed python in other language like C?
4. How to extend python with C language?
All above questions more or less fall in the same category, and roughly speaking trying to achieve same objective.
However, every time I have been asked above questions, first thing I did to identify the objective as to what particular is trying to achieve. Because most of the time, based on one's experience, job profile (like tester or coder) and programming language he is familiar with (if he is C or python programmer); one may ask how he can do X, but in reality his intention or objective is altogether a different.
So based on my experience so far, one can consider below things whenever he want python to talk to external program.
- At simplest, probably one might need to call a binary, within from the python. Or vice-a-versa python file from the c-program. Then one need not to bother about embedding or extending python.
Call a binary from python:
cmd = 'bin/bar --option --otheroption'
os.system(cmd) # returns the exit status
args = ("bin/bar", "-c", "somefile.xml", "-d", "text.txt", "-r", "aString", "-f", "anotherString")
#args = "bin/bar -c somefile.xml -d text.txt -r aString -f anotherString".split()
popen = subprocess.Popen(args, stdout=subprocess.PIPE)
output = popen.stdout.read()
Call a python program within from c program
- Now suppose, you already have python library and want to call any random function from that library. (in other words you want to embed python in C or other language)
Embedding Python in antoher language
int main(int argc, char *argv)
Py_SetProgramName(argv); /* optional but recommended */
PyRun_SimpleString("from time import time,ctime\n"
"print 'Today is',ctime(time())\n");
/*Or if you want to run python file within from the C code*/
- Now lets talk about reverse case of above, that is you want to call C functions within from python program. If you want your C code, from then be part of your python it self, this can be called extending python's functionalities. (Extending python using C is only possible in CPython version of the python)
Extending python can do two things that can’t be done directly in Python:
1. they can implement new built-in object types, and
2. they can call C library functions and system calls
Extending python can be done using Python APIs. However, this is not portable across all versions of the python, moreover its too complicated!. Hence it is advisable to use ctype, which is btw portable across various versions.
A windows example:
>> from ctypes import *
>> libc = cdll.msvcrt
>> print libc.time(None)
>> printf = libc.printf
>> printf("Hello, %s\n", "World!")
>> Hello, World!
>> printf("%d bottles of beer\n", 42)
>> 42 bottles of beer
This article is in no way a complete guide for extending/embedding python. For detailed explanation of above topics one might refer to: