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
🌐
Real Python
realpython.com › c-for-python-programmers
C for Python Programmers – Real Python
January 25, 2023 - Free Download: Get a sample chapter from CPython Internals: Your Guide to the Python 3 Interpreter showing you how to unlock the inner workings of the Python language, compile the Python interpreter from source code, and participate in the development of CPython. The preprocessor, as the name suggests, is run on your source files before the compiler runs. It has very limited abilities, but you can use them to great advantage in building C programs.
🌐
University of Toronto
cs.toronto.edu › ~patitsas › cs190 › c_for_python.html
C for Python Programmers
One major difference between C and Python is simply how you go about executing programs written in the two languages. With C programs, you usually use a compiler when you are ready to see a C program execute; by contrast, with Python, you typically use an interpreter.
Discussions

Can we use C code in Python? - Stack Overflow
I know that Python provides an API so you can call Python interpreter in C code, but what I want is the opposite. My program needs to use some C API, so the code must be written in C. But I also w... More on stackoverflow.com
🌐 stackoverflow.com
How are Python and C related? I've read that Python is 'made from C'. Does that mean that Python is just an abstraction of lots of large C functions?
Python runs on an interpreter* called CPython. The interpreter takes in Python code (,compiles it into an internal representation) and runs it. CPython, as the name suggests is written in C. So, when you run a Python script like "print(5+5)", CPython, the program you usually run with the python command, reads the line, lexes it into Tokens like print, (, 5, +, 5, ) then transforms it a couple more times, until it ends up with a bytecode representation, something like: LITERAL 5, LITERAL 5, ADD, PRINT, where LITERAL pushes the number 5 onto the "stack", ADD takes the two items highest on the stack, removes them from it, and pushes their sum back onto it and PRINT removes the top item from the stack and prints it. So it works like this: INSTRUCTION: {} STACK: [] INSTRUCTION: LITERAL STACK: [5] INSTRUCTION: LITERAL STACK: [5, 5] INSTRUCTION: ADD STACK: [10] INSTRUCTION: PRINT STACK: [] stdio: "10" It's not an abstraction in C per se, as in, a C compiler can be not involved in the life of a Python program. The interpreter is compiled on someone else's computer, you can download it, and it will work without a C compiler. CPython doesn't compile Python to C, so there never is a point where your python program is a C program. *- that's the most commonly used implementation anyway More on reddit.com
🌐 r/learnprogramming
25
70
August 29, 2022
C or Python?
Learning how to program isn't about choosing the most useful language to learn. It's about developing the mindset of a programmer and understanding the broad concepts that span across every language. In my opinion, C is the best language to learn first. C's design pioneered the languages we have today. By studying C first you will understand the roots of object oriented programming and have a better appreciation for languages like Python, Java, C++ ect... More on reddit.com
🌐 r/learnprogramming
61
57
July 21, 2021
How hard is it going to be to learn C after python ?
It for sure is manageable and you will get used to it. Already knowing a programming language will help a lot (C was my fist programming language) the syntax of python was always a bit weird for me as im used to C so it could be similar for you wit the C syntax maybe. But i think it's quite intuitive once u got used to it. Also you are usually closer to the hardware when writing code but also not nearly as close as assembly for example so i think it will take a bit of practice but you will get used to it. More on reddit.com
🌐 r/C_Programming
23
8
August 2, 2020
🌐
GeeksforGeeks
geeksforgeeks.org › python › using-c-codes-in-python-set-1
Using C codes in Python | Set 1 - GeeksforGeeks
July 11, 2025 - As it is very evident that many of Python’s built-in libraries are written in C. So, to access C is a very important part of making Python talk to existing libraries. There is an extensive C programming API that Python provides but there are many different to deal with C.
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.

Top answer
1 of 12
159
Python runs on an interpreter* called CPython. The interpreter takes in Python code (,compiles it into an internal representation) and runs it. CPython, as the name suggests is written in C. So, when you run a Python script like "print(5+5)", CPython, the program you usually run with the python command, reads the line, lexes it into Tokens like print, (, 5, +, 5, ) then transforms it a couple more times, until it ends up with a bytecode representation, something like: LITERAL 5, LITERAL 5, ADD, PRINT, where LITERAL pushes the number 5 onto the "stack", ADD takes the two items highest on the stack, removes them from it, and pushes their sum back onto it and PRINT removes the top item from the stack and prints it. So it works like this: INSTRUCTION: {} STACK: [] INSTRUCTION: LITERAL STACK: [5] INSTRUCTION: LITERAL STACK: [5, 5] INSTRUCTION: ADD STACK: [10] INSTRUCTION: PRINT STACK: [] stdio: "10" It's not an abstraction in C per se, as in, a C compiler can be not involved in the life of a Python program. The interpreter is compiled on someone else's computer, you can download it, and it will work without a C compiler. CPython doesn't compile Python to C, so there never is a point where your python program is a C program. *- that's the most commonly used implementation anyway
2 of 12
14
Python and C are not directly related. The official Python interpreter named CPython is written in C, but that has nothing to do with the Python language itself. Other people have written other Python interpreters in other languages.
🌐
Python
docs.python.org › 3 › extending › extending.html
1. Extending Python with C or C++ — Python 3.14.3 documentation
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.
🌐
Aimore Technologies
aimoretechnologies.com › software training institute in chennai › python insights › python vs c: differences, pros & cons decoded
Python vs C: Differences, Pros & Cons Decoded
November 4, 2025 - In contrast, C is a low-level and compiled language that offers fine-grained control over system resources. It is commonly used in systems programming, embedded systems, and performance-critical applications.
Find elsewhere
🌐
Hacettepe
web.cs.hacettepe.edu.tr › ~bbm101 › fall16 › lectures › w12-c-for-python-programmers.pdf pdf
C for Python Programmers BBM 101 - Introduction to Programming I
Python · • Created by Guido van Rossum in the late 1980s · • Allows programming in multiple paradigms: object- oriented, structured, functional · • Uses dynamic typing and garbage collection · Slide credit: Thomas J. Cortina · Building a simple program in C ·
🌐
DEV Community
dev.to › rapidnerd › using-python-and-c-together-d4e
Using Python and C together - DEV Community
August 8, 2018 - How to use Python and C in the same project. Tagged with python, c, tutorial.
🌐
Unstop
unstop.com › home › blog › 20+ differences between c and python explained with examples
20+ Differences Between C and Python Explained With Examples
August 30, 2024 - C is a compiled language that directly translates code into machine code for faster performance, while Python is an interpreted language that executes code line-by-line, which introduces overhead and generally results in slower performance.
🌐
InfoWorld
infoworld.com › home › software development › programming languages › python
PythoC: A new way to generate C code from Python | InfoWorld
December 19, 2025 - Generating C code with Python has typically involved libraries like Cython, which use type-annotated Python code to generate C extension modules for Python. A new project, PythoC, takes a different approach.
🌐
Rose-Hulman Institute of Technology
rose-hulman.edu › class › cs › csse120 › Resources › C › Python_vs_C.html
Python and C -- Comparisons and Contrasts
/* Simple C program */ #include <stdio.h> int main() { int year = 2007; printf("Hello World!\n"); /* Notice that C's printf does not automatically put in a newline */ printf("CSSE 120 changed a lot in %d.\n", year); return 0; }
🌐
DigitalOcean
digitalocean.com › community › tutorials › calling-c-functions-from-python
Calling C Functions from Python | DigitalOcean
August 3, 2022 - If you change the C program file, you will have to regenerate the shared library file. The python default implementation is written in C programming and it’s called CPython. So it’s not very uncommon to use C functions in a python program.
🌐
Medium
medium.com › @raihansltn › why-is-c-faster-than-python-os-level-perspective-04d6e485e3ae
Why is C Faster Than Python? OS-Level Perspective | by Raihan Sultan Pasha Basuki | Medium
March 15, 2025 - I classify some key differences between C and Python implementation ... Both programs count prime numbers up to 250.000 and print the result, the C version is significantly faster because it is compiled to machine code, yet the Python version is slower because it’s interpreted line by line.
🌐
Reddit
reddit.com › r/learnprogramming › c or python?
C or Python? : r/learnprogramming
July 21, 2021 - Python definitely. The libraries that you'll be using will probably be written in C so they run fast, but you don't need to worry about the actual C-code. C is a (relatively) low-level systems programming language invented in the early 1970s.
🌐
Wikipedia
en.wikipedia.org › wiki › CPython
CPython - Wikipedia
3 weeks ago - CPython is the reference implementation of the Python programming language. Written in C and Python, CPython is the default and most widely used implementation of the Python language.
🌐
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
The CPython interpreter (aka, "python") works by compiling Python source code to intermediate bytecodes, and then operating on those. CPython, which is written in C, is also accompanied by an Application Programming Interface (API) that enables communication between Python and C (and thus basically ...