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 Overflow
🌐
Quora
quora.com › How-do-I-write-a-Python-script-to-run-a-C-program
How to write a Python script to run a C program - Quora
Answer (1 of 6): I assume you want to both compile and run the code! You can follow following steps: Since i am using windows to run the "gcc" command i did these settings: 1. Install DEV-CPP 2. Now set the path of "gcc" file in your environmental variables i.e. "C:\Program Files (x86)\Dev-Cpp\M...
Discussions

linux - How to make a call to an executable from Python script? - Stack Overflow
Hi Peter, there is the error: bin/bar: bin/bar: cannot execute binary file and without any other information from the Python runtime. What is the cause? More on stackoverflow.com
🌐 stackoverflow.com
Can we use C code in Python? - Stack Overflow
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. More on stackoverflow.com
🌐 stackoverflow.com
How do you call Python code from C code? - Stack Overflow
Only problem is due to all the ... on the binary representation of the structs just memcopy and casting the raw data off the socket. 2009-06-29T02:18:06.58Z+00:00 ... Save this answer. ... Show activity on this post. 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. ... Indeed if he wants to run Python in ... More on stackoverflow.com
🌐 stackoverflow.com
How to compile the Python code to executable binary file like Linux C and Golang
Hi, how can I compile the python files to an executable binary file like Linux C and Golang in different platforms such as Linux, Mac and Windows? More on discuss.python.org
🌐 discuss.python.org
1
1
July 25, 2023
🌐
howtos
wgilpin.com › howto › howto_cython.html
Running a C/C++ executable from within Python, without any IO | howtos
Now - Compile your executable, and then use the subprocess library. Then, in your Python script, use some variant of this: def test_no_io(): ''' Run a process that takes no input and produces no output ''' ## Shell=False helps the process terminate process = subprocess.Popen("./hello", shell=False) ## Get exit codes out, err = process.communicate() errcode = process.returncode print(errcode) process.kill() process.terminate()
🌐
Real Python
realpython.com › python-bindings-overview
Python Bindings: Calling C or C++ From Python – Real Python
March 18, 2026 - It has a similar purpose to make but uses Python instead of Makefiles. You’ll need to install invoke in your virtual environment using pip: ... $ invoke build-cmult ================================================== = Building C Library * Complete · To see which tasks are available, you use the --list option: ... $ invoke --list Available tasks: all Build and run all tests build-cffi Build the CFFI Python bindings build-cmult Build the shared library for the sample C code build-cppmult Build the shared library for the sample C++ code build-cython Build the cython extension module build-pybind11 Build the pybind11 wrapper library clean Remove any built objects test-cffi Run the script to test CFFI test-ctypes Run the script to test ctypes test-cython Run the script to test Cython test-pybind11 Run the script to test PyBind11
🌐
DigitalOcean
digitalocean.com › community › tutorials › calling-c-functions-from-python
Calling C Functions from Python | DigitalOcean
Get paid to write technical tutorials and select a tech-focused charity to receive a matching donation. ... Full documentation for every DigitalOcean product. ... The Wave has everything you need to know about building a business, from raising funding to marketing your product. ... Scale up as you grow — whether you're running one virtual machine or ten thousand.
🌐
Python
docs.python.org › 3 › extending › extending.html
1. Extending Python with C or C++ — Python 3.14.7 documentation
It is quite easy to add new built-in modules to Python, if you know how to program in C. Such extension modules can do two things that can’t be done directly in Python: they can implement new built-in object types, and they can call C library functions and system calls. To support extensions, the Python API (Application Programmers Interface) defines a set of functions, macros and variables that provide access to most aspects of the Python run-time system.
Find elsewhere
🌐
GeeksforGeeks
geeksforgeeks.org › how-to-call-a-c-function-in-python
How to Call a C function in Python | GeeksforGeeks
November 1, 2023 - Have you ever came across the situation where you have to call C function using python? This article is going to help you on a very basic level and if you have not come across any situation like this, you enjoy knowing how it is possible. First, let's write one simple function using C and generate a shared library of the file.
🌐
GeeksforGeeks
geeksforgeeks.org › calling-python-from-c-set-1
Calling Python from C | Set 1 - GeeksforGeeks
March 27, 2019 - Previous Python Articles (Set 1 | Set 2 | Set 3 | Set 4 | Set 5) This article is focused on command line arguments as well as variable arguments (args and kwargs) for the functions in python. Command Line Arguments Till now, we have taken input in python using raw_input() or input() [for integers]. ... In C programming, setting a bit is the process of setting a specific bit of a binary number to 1.
🌐
Readthedocs
reptate.readthedocs.io › developers › python_c_interface.html
Tutorial: Interfacing Python and C code — RepTate 1.4.0 documentation
We can cite Cython or Numba that transform Python code into C executable and require minimal addition to the existing Python code. There is also Ctypes that provides C compatible data types, and allows calling functions from external libraries, e.g. calling pre-compiled C functions. It is a very effective means to communicate with existing C code. ... All these solutions require a C compiler installed on your machine. In RepTate, some theories (most notably the React application theories) are written in C code and interfaced with Python using, Ctypes.
🌐
Surrey
ri0005.pages.surrey.ac.uk › binary_c-python
Welcome to binary_c-python’s documentation! — binary_c-python documentation
Python 3.7 or higher (3.6 is EOL, and we are using 3.9 for development) ... The packages that are required for this code to run are listed in the requirements.txt, which automatically gets read out by setup.py · Before compilation you need to have certain environment variables: ... LD_LIBRARY_PATH should include $BINARY_C/src and whatever directories are required to run binary_c (e.g.
Top answer
1 of 2
128

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.

2 of 2
16

You don't necessary need to extend Python (which is not trivial, btw), but can use foreign function interface such as ctypes.

🌐
Quora
quora.com › Can-I-execute-a-c-file-from-inside-the-code-of-a-py-file
Can I execute a .c file from inside the code of a .py file? - Quora
Answer (1 of 3): No, you can’t. *.c files need to be compiled and cannot be run by themselves. They can only be turned into programs if they contain main() function, and are very likely to require extra parameters to be passed to the compiler (include/library paths) Now, you can call compiled c...
🌐
Klein Embedded
kleinembedded.com › home › calling c code from python
Calling C code from Python - Klein Embedded
January 31, 2023 - To create Python bindings for C or C++ code, there a several packages to choose from such as ctypes, CFFI, Cython, PyBind11, Boost.Python and more.
🌐
Python.org
discuss.python.org › python help
How to compile the Python code to executable binary file like Linux C and Golang - Python Help - Discussions on Python.org
July 25, 2023 - Hi, how can I compile the python files to an executable binary file like Linux C and Golang in different platforms such as Linux, Mac and Windows?
🌐
Medium
medium.com › nabla-squared › how-to-use-c-code-in-python-582491e572d1
How to use C code in Python. … and should we do this? | by Dorian Lazar | Nabla Squared | Medium
May 31, 2021 - You can download and install it from here. After installation, simply replace python with pypy when executing a Python script.