There is no such thing as a C script. If you meant a C program you need to compile spa.c and spa.h into an executable before running it.
If you use GCC in Linux or Mac OS X:
$ gcc -Wall spa.c -o spa
Will get you an executable named spa.
After that, you can run spa program from your Python script with:
from subprocess import call
call(["./spa", "args", "to", "spa"])
Answer from Pablo Santa Cruz on Stack OverflowThere is no such thing as a C script. If you meant a C program you need to compile spa.c and spa.h into an executable before running it.
If you use GCC in Linux or Mac OS X:
$ gcc -Wall spa.c -o spa
Will get you an executable named spa.
After that, you can run spa program from your Python script with:
from subprocess import call
call(["./spa", "args", "to", "spa"])
cinpy comes close using the awesome combination of tcc and ctypes
The following code is ripped from cinpy_test.py included in the package.
import ctypes
import cinpy
# Fibonacci in Python
def fibpy(x):
if x<=1: return 1
return fibpy(x-1)+fibpy(x-2)
# Fibonacci in C
fibc=cinpy.defc("fib",
ctypes.CFUNCTYPE(ctypes.c_long,ctypes.c_int),
"""
long fib(int x) {
if (x<=1) return 1;
return fib(x-1)+fib(x-2);
}
""")
# ...and then just use them...
# (there _is_ a difference in the performance)
print fibpy(30)
print fibc(30)
linux - How to make a call to an executable from Python script? - Stack Overflow
Can we use C code in Python? - Stack Overflow
How do you call Python code from C code? - Stack Overflow
How to compile the Python code to executable binary file like Linux C and Golang
For executing the external program, do this:
import subprocess
args = ("bin/bar", "-c", "somefile.xml", "-d", "text.txt", "-r", "aString", "-f", "anotherString")
#Or just:
#args = "bin/bar -c somefile.xml -d text.txt -r aString -f anotherString".split()
popen = subprocess.Popen(args, stdout=subprocess.PIPE)
popen.wait()
output = popen.stdout.read()
print output
And yes, assuming your bin/bar program wrote some other assorted files to disk, you can open them as normal with open("path/to/output/file.txt"). Note that you don't need to rely on a subshell to redirect the output to a file on disk named "output" if you don't want to. I'm showing here how to directly read the output into your python program without going to disk in between.
The simplest way is:
import os
cmd = 'bin/bar --option --otheroption'
os.system(cmd) # returns the exit status
You access the files in the usual way, by using open().
If you need to do more complicated subprocess management then the subprocess module is the way to go.
I want to invoke those C function or executables in python. Is that possible.
Yes, you can write C code that can be imported into Python as a module. Python calls these extension modules. You can invoke it from Python directly, an example from the documentation:
Python Code
import example
result = example.do_something()
C Code
static PyObject * example(PyObject *self)
{
// do something
return Py_BuildValue("i", result);
}
If I want the C code to be a library, which means I use it with #include and linkage of the *.o likely in python, how to do it or is that possible.
You build it as a shared library *.dll or *.so You can also investigate using distutils to distribute your module.
If I write the C code into executable, which means it becomes a command, can I invoke it in python directly?
If you write a *.exe then you are doing the opposite (invoking Python from C). The method you choose (exe vs shared library) depends on if you want a "C program with some Python" or a "Python program with some C".
Also, I heard that python code can be compiled, does that mean we can execute the code without the source file? Are the output files binary files? Does it improve performance?
Python reads *.py files and compiles to *.pyc bytecode files when you run it. The bytecode is then run in the Python virtual machine. This means "executing the same file is faster the second time as recompilation from source to bytecode can be avoided." (from the Python glossary) So if you haven't edited your *.py files, it will run the *.pyc. You can distribute *.pyc files without *.py files, however they are not encrypted and can be reverse-engineered.
You don't necessary need to extend Python (which is not trivial, btw), but can use foreign function interface such as ctypes.
I recommend the approaches detailed here. It starts by explaining how to execute strings of Python code, then from there details how to set up a Python environment to interact with your C program, call Python functions from your C code, manipulate Python objects from your C code, etc.
EDIT: If you really want to go the route of IPC, then you'll want to use the struct module or better yet, protlib. Most communication between a Python and C process revolves around passing structs back and forth, either over a socket or through shared memory.
I recommend creating a Command struct with fields and codes to represent commands and their arguments. I can't give much more specific advice without knowing more about what you want to accomplish, but in general I recommend the protlib library, since it's what I use to communicate between C and Python programs (disclaimer: I am the author of protlib).
Have you considered just wrapping your python application in a shell script and invoking it from within your C application?
Not the most elegant solution, but it is very simple.