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.

Answer from Jacqui Gurto on Stack Overflow
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.

🌐
InfoWorld
infoworld.com › home › software development › programming languages › python
PythoC: A new way to generate C code from Python | InfoWorld
December 19, 2025 - A new project, PythoC, takes a different approach. It uses type-hinted Python to programmatically generate C code—but chiefly for standalone use, and with many more compile-time code generation features than Cython has.
People also ask

How do I convert Python to C?
Paste your Python code into the input box, confirm the languages are set to Python and C, and click Convert. CodeConvert AI analyzes your Python code and generates equivalent C code in seconds, preserving the original logic and structure.
🌐
codeconvert.ai
codeconvert.ai › home › convert from python › python to c converter
Free Python to C Converter - AI Code Translation | CodeConvert AI
Can I convert C back to Python?
Yes. CodeConvert AI converts in both directions, so you can convert C to Python just as easily using our C to Python converter. Try the C to Python Converter
🌐
codeconvert.ai
codeconvert.ai › home › convert from python › python to c converter
Free Python to C Converter - AI Code Translation | CodeConvert AI
Can I convert an entire Python project to C?
You can convert Python files one at a time by pasting each file's code. For a full migration, convert each file and then review how classes, dependencies, and project structure map from Python to C. Signing in for free raises the input limit to 25,000 characters per conversion for larger files.
🌐
codeconvert.ai
codeconvert.ai › home › convert from python › python to c converter
Free Python to C Converter - AI Code Translation | CodeConvert AI
🌐
CodeConvert AI
codeconvert.ai › home › convert from python › python to c converter
Free Python to C Converter - AI Code Translation | CodeConvert AI
A well commented function to check if a number if odd or even. ... Paste your Python code into the input box, confirm the languages are set to Python and C, and click Convert.
🌐
Readthedocs
reptate.readthedocs.io › developers › python_c_interface.html
Tutorial: Interfacing Python and C code — RepTate 1.4.0 documentation
Last steps, we need to modify the Python code. We make some addition to the file “basic_function_helper.py”. We need to: ... # Callback stuff from ctypes import CFUNCTYPE, POINTER def get_percent(percent): """Print advancement and set the next call when C has advanced a further 20%""" self.Qprint("Advancement of C calculations: %f%%" % (percent*100)) return percent + 0.2 CB_FTYPE_DOUBLE_DOUBLE = CFUNCTYPE(c_double, c_double) # define C pointer to a function type cb_get_percent = CB_FTYPE_DOUBLE_DOUBLE(get_percent) # define a C function equivalent to the python function "get_percent" basic_function_lib.def_python_callback(cb_get_percent) # the the C code about that C function
🌐
Python
docs.python.org › 3 › extending › extending.html
1. Extending Python with C or C++ — Python 3.14.7 documentation
The C extension interface is specific to CPython, and extension modules do not work on other Python implementations. In many cases, it is possible to avoid writing C extensions and preserve portability to other implementations. For example, if your use case is calling C library functions or system calls, you should consider using the ctypes module or the cffi library rather than writing custom C code.
🌐
Reddit
reddit.com › r/learnpython › how is python able to use c code?
r/learnpython on Reddit: How is python able to use c code?
September 18, 2021 -

I was always curious how python modules in c work. I know how to write and use a python c module, but i dont know how it works internally. Can anyone put me in the right path where can i start, an article or sthm or try to explain. And are there any simple tutorials to implement that sort of thing. Like calling .so files from your language etc...

Top answer
1 of 1
3
There is an API in the C that you can use to interact with Python objects from C code, which is the same API that python uses internally to bootstrap itself in many cases. There are good guides in the official python docs for how to make a python module in C in detail, but the overview of how it's done is: Create a c source file Include the Python.h header file, which exists on any system with the python interpreter installed. There are functions and macros in the Python.h header that allow you to create a python module "object", which can be imported, and to specify which functions should be part of that object, and for each function, you can specify it's python function signature, although from a C perspective, all the values passed into a python function are just python objects (pointers to a PyObject struct) The PyObject structure is the fundamental data type inside the python interpreter. It is a polymorphic struct, which is really neat. You can implement polymorphism and object orientation inside of C with some neat struct casting tricks, and that's what python does. Therefore, there are macros to convert the PyObject struct coming into your function to a PyListObject struct, for example, and then there are more C APIs for doing what you need to do to a list (append, insert, delete, etc.) Now, once your C code is all written up, you need to compile it. This would be tricky, except that python's setuptools (setup.py) has utilities for including C extensions in your library. So, you can just compile it through that tool while mostly ignoring the complexity of compiling and linking manually. At the end of the day, if you created a module named foo in C, you should be able to simply import and use it like any other python module. For example, the csv module is implemented entirely in C, although you'd probably never know it!
Find elsewhere
🌐
GitHub
github.com › cython › cython
GitHub - cython/cython: The most widely used Python to C compiler · GitHub
Cython translates Python code to C/C++ code, but additionally supports calling C functions and declaring C types on variables and class attributes.
Author: cython
🌐
GeeksforGeeks
geeksforgeeks.org › python › using-c-codes-in-python-set-1
Using C codes in Python | Set 1 - GeeksforGeeks
July 11, 2025 - Using ctypes : Python ctypes will come to play but make sure the C code, that is to be converted, has been compiled into a shared library that is compatible with the Python interpreter (e.g., same architecture, word size, compiler, etc.). Further the libsample.so file has been placed in the same directory as the work.py.
🌐
Real Python
realpython.com › build-python-c-extension-module
Building a Python C Extension Module – Real Python
March 18, 2026 - By default, the Python interpreter uses clang for compiling the C code. If you want to use gcc or any other C compiler for the job, then you need to set the CC environment variable accordingly, either inside the setup script or directly on the ...
🌐
DigitalOcean
digitalocean.com › community › tutorials › calling-c-functions-from-python
Calling C Functions from Python | DigitalOcean
Technical tutorials, Q&A, events — This is an inclusive place where developers can find or lend support and discover new ways to contribute to the community.
🌐
Real Python
realpython.com › c-for-python-programmers
C for Python Programmers – Real Python
March 18, 2026 - Unlike in Python, whitespace isn’t important to the C compiler. The compiler doesn’t care if you split statements across lines or jam your entire program into a single, very long line. This is because it uses delimiters for all statements and blocks. There are, of course, very specific rules for the parser, but in general you’ll be able to understand the CPython source just knowing that each statement ends with a semicolon (;), and all blocks of code are surrounded by curly braces ({}).
🌐
Python Tips
book.pythontips.com › en › latest › python_c_extension.html
22. Python C extensions — Python Tips 0.1 documentation
For other types such as boolean or float, we have to use the correct ctypes. This is seen while passing parameters to the adder.add_float(). We first create the required c_float types from python decimal values, and then use them as arguments to the C code. This method is simple and clean, but limited.
🌐
Cornell Virtual Workshop
cvw.cac.cornell.edu › python-performance › intro › cpython-api
Cornell Virtual Workshop > Python for High Performance > Overview > CPython and the Python/C API
CPython, which is written in C, ... to any other language that C can interoperate with). The Python/C API allows for compiled pieces of code to be called from Python programs or executed within the CPython interpreter....
🌐
Real Python
realpython.com › python-bindings-overview
Python Bindings: Calling C or C++ From Python – Real Python
March 18, 2026 - Complex numbers are numbers with an imaginary part. While Python has built-in complex numbers, and C has complex numbers, there’s no built-in method for marshalling between them. To marshal complex numbers, you’ll need to build a struct or class in the C code to manage them.
🌐
Python Tutor
pythontutor.com › visualize.html
Visualize code in Python, JavaScript, C, C++, and Java
Free online compiler and visual debugger for Python, Java, C, C++, and JavaScript. Step-by-step visualization with AI tutoring.
🌐
AskPython
askpython.com › python › examples › calling-python-scripts-from-c
Calling Python Scripts from C: A Step-by-Step Guide Using Python/C API - AskPython
April 10, 2025 - It provides a step-by-step guide on setting up the API, creating Python and C files, initializing the interpreter, creating Python objects, calling Python functions from C, and finalizing the interpreter. The article also discusses the advantages of integrating Python code in C and different compilation methods.
🌐
CodeConvert AI
codeconvert.ai › home › convert from c › c to python converter
Free C to Python Converter - AI Code Translation | CodeConvert AI
C and Python differ in syntax, type system, standard libraries, and common idioms, so copying code line for line usually will not compile. The converter maps each C construct to its closest Python equivalent.
🌐
Quora
quora.com › How-can-I-run-C-code-in-Python
How to run C code in Python - Quora
Answer (1 of 8): I am assuming that you want to run your C code using Python language(interpreter) not a Python script to run a .c or .exe file. You can do it by calling subprocess for that you will need subprocess and os module. [code]import sys import subprocess import os prog = r''' #includ...
🌐
DEV Community
dev.to › erikwhiting88 › how-to-use-c-functions-in-python-7do
How to Use C Functions in Python - DEV Community
August 4, 2019 - Note, the way to call functions inside the imported C shared object file is by saying <CDLL Object>.<function name from C code>(<parameter>). Easy! So basically, any time we want to use that C function within Python, we call the factorial function which will run the C function with the parameter passed in by the user and evaluate the result.