Python reference implementation
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. CPython can be defined … Wikipedia
Factsheet
Original author Guido van Rossum
Developers Python core developers and the Python community, supported by the Python Software Foundation
Initial release 26 January 1994; 32 years ago (1994-01-26)
Factsheet
Original author Guido van Rossum
Developers Python core developers and the Python community, supported by the Python Software Foundation
Initial release 26 January 1994; 32 years ago (1994-01-26)
🌐
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. CPython can be defined as both an interpreter and a compiler as it compiles Python code into bytecode before ...
🌐
Reddit
reddit.com › r › Python
r/Python
January 25, 2008 - I was wondering what’s most popular now in the Python world. Building applications with FastAPI and a frontend framework, or building an application with a ‘batteries included’ framework like Django. ... Today we’re announcing that OpenAI will acquire Astral⁠(opens in a new window), bringing powerful open source developer tools into our Codex ecosystem.
Discussions

How to use the -c flag in python - Stack Overflow
I noticed in the python doc that there is a -c flag. Here is what python doc says: Execute the Python code in command. command can be one or more statements separated by newlines, with signific... More on stackoverflow.com
🌐 stackoverflow.com
Understanding what CPython actually IS has greatly enhanced my understanding of Python.
Potential errors just happen as it goes through each line of code, one by one. However its also important to understand that Python is actually still semi-compiled into "bytecode", I think it's helpful, and not too difficult, to notice the difference between run-time and compile-time errors. You could try print("hello") 5/0 print("world") vs the same thing with a tiny addition print("hello") 5/0 print("world") ( In the first example we'll print once before hitting a ZeroDivisionError, which is what people imagine when they imagine interpreting line by line. In the second example we'll print nothing, because we hit a SyntaxError before ever starting to run. Even though the error is "after" the second print, it's a different kind of error than what we saw previously. Of course real errors are more complex, but you can benefit from knowing which ones occur in the course of a live program vs which ones prevent the program from living. More on reddit.com
🌐 r/learnpython
35
125
May 25, 2024
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
How do you call Python code from C code? - Stack Overflow
I want to extend a large C project with some new functionality, but I really want to write it in Python. Basically, I want to call Python code from C code. However, Python->C wrappers like SWIG a... More on stackoverflow.com
🌐 stackoverflow.com
🌐
Python
python.org › about
About Python™ | Python.org
Python can be easy to pick up whether you're a first time programmer or you're experienced with other languages.
🌐
Python
docs.python.org › 3 › extending › extending.html
1. Extending Python with C or C++ — Python 3.14.4 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.
🌐
Python
docs.python.org › 3 › c-api › index.html
Python/C API reference manual — Python 3.14.4 documentation
This manual documents the API used by C and C++ programmers who want to write extension modules or embed Python. It is a companion to Extending and Embedding the Python Interpreter, which describes...
🌐
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, ... 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 › c-for-python-programmers
C for Python Programmers – Real Python
January 25, 2023 - In this tutorial, you'll learn the basics of the C language, which is used in the source code for CPython, the most popular Python implementation. Learning C is important for Python programmers interested in contributing to CPython.
Find elsewhere
🌐
Reddit
reddit.com › r/learnpython › understanding what cpython actually is has greatly enhanced my understanding of python.
r/learnpython on Reddit: Understanding what CPython actually IS has greatly enhanced my understanding of Python.
May 25, 2024 -

First off, its perfectly understandable to not really care about language theory as a beginner. This stuff is not necessary to learn to code.

However, after recently doing some deep dives on what CPython really is and how it works, I have found the knowledge to be extremely enlightening. And it has really opened my eyes as to how Python is used, and why its used in the places it is.

For those who are unaware, allow me to share what I've learned.

So the key piece of information is that CPython is, at its core, a program written in C. Its purpose is to take Python code as input, then convert that Python into its own native instructions (written in C), and then execute them. And perhaps most importantly, it does this in a line-by-line manner. That just means it doesn't try to error check the entire program before running it. Potential errors just happen as it goes through each line of code, one by one.

However its also important to understand that Python is actually still semi-compiled into "bytecode", which is an intermediate stage between Python and full machine code. CPython converts your python scripts into bytecode files first, so what it actually runs is the bytecode files.

Now where it gets super interesting is that CPython is not the only "implementation" of Python (implementation means some kind of program, or system, that takes Python code as input and does something with it). More on that later.

On the subject of bytecode, it naturally leads to some other interesting questions, such as "Can I share the bytecode files?", to which the answer is no. That's one of the key aspects of CPython. The bytecode is "not platform agnostic". (I'm really sorry if that's not the correct term, I just learned all this stuff recently). That means the bytecode itself is compiled for your specific environment (the python version and dependencies). The reason for this is that its part of Python's design philosophy to be constantly improving the bytecode.

Once you understand that you can then comprehend what other implementations of Python do. PyPy for instance aims to make a Python running environment that works more like Java, where it performs "just-in-time" compilation to turn the bytecode into native machine code at runtime, and that's why it can make certain things run faster. Then you have the gamut of other ways Python can be used, such as:

  • Cython - aims to translate Python into C, which can then be compiled

  • Nuitka - aims to translate Python into C++, which is more versatile and less restrictive

  • Jython - this semi-compiles Python into Java bytecode that can be run in a Java virtual machine/runtime

  • IronPython - semi-compiles Python into C# bytecode, for running in .NET runtime

  • PyPy - A custom JIT-compiler that works in a manner philosophically similar to Java

  • MicroPython - a special version of python that's made for embedded systems and 'almost' bare-metal programming

Oh and then there's also the fact that if you want to use Python for scripting while working in other languages, its important to understand the difference between calling CPython directly, or using "embedded" CPython. For instance some game coders might opt to just call CPython as an external program. However some might opt to just build CPython directly into the game itself so that it does not need to. Different methods might be applicable to different uses.

Anyway all of this shit has been very entertaining for me so hopefully someone out there finds this interesting.

🌐
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.
🌐
Readthedocs
reptate.readthedocs.io › developers › python_c_interface.html
Tutorial: Interfacing Python and C code — RepTate 1.3.15 documentation
In our case, it is void, which translates in Python to None. See fundamental-data-types for a list of equivalence. Our C function c_square accepts three arguments: an int and two double *. Hence, our Python function python_c_square accepts three arguments too but they must by of type c_int and “array of c_double” defined by the ctypes Python library.
🌐
GeeksforGeeks
geeksforgeeks.org › python › using-c-codes-in-python-set-1
Using C codes in Python | Set 1 - GeeksforGeeks
July 11, 2025 - Prerequisite: How to Call a C function in Python Let's discuss the problem of accessing C code from Python. 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.
🌐
CodeConvert AI
codeconvert.ai › c-to-python-converter
Free C to Python Converter — AI Code Translation | CodeConvert AI
Simply paste your C code into the input box and click the Convert button. Our AI will analyze your C code and produce equivalent Python code in seconds, preserving the original logic and structure.
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.
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 3
145

From everything I've seen, it's a combination of practical and historical reasons. The (mostly) historical reason is that CPython 1.0 was released in 1989. At that time, C was just recently standardized. C++ was almost unknown and decidedly non-portable, because almost nobody had a C++ compiler.

Although C++ is much more widespread and easily available today, it would still take a fair amount of work to rewrite CPython into the subset of C that's compatible with C++. By itself, that work would provide little or no real benefit.

It's a bit like Joel's blog post about starting over and doing a complete rewrite being the worst mistake a software company can make. I'd counter that by pointing to Microsoft's conversion from the Windows 3.0 core to the Windows NT core, and Apple's conversion from MacOS 9 to Mac OS/X. Neither one killed the company -- but both were definitely large, expensive, long-term projects. Both also point to something that's crucial to success: maintaining both code bases for long enough that (most) users can switch to the new code base at their leisure, based on (at least perceived) benefits.

For a development team the size of Python's, however, that kind of change is much more difficult. Even the change from Python 2 to 3 has taken quite a bit of work, and required a similar overlap. At least in that case, however, there are direct benefits to the changes, which rewriting into C++ (by itself) wouldn't (at least immediately) provide.

Linus Torvalds's rant against C++ was brought up, so I'll mention that as well. Nothing I've seen from Guido indicates that he has that sort of strong, negative feelings toward C++. About the worst I've seen him say is that teaching C++ is often a disaster -- but he immediately went on to say that this is largely because the teachers didn't/don't know C++.

I also think that while it's possible to convert a lot of C code to C++ with relative ease, that getting much real advantage from C++ requires not only quite a bit more rewriting than that, but also requires substantial re-education of most developers involved. Most well-written C++ is substantially different from well-written C to do the same things. It's not just a matter of changing malloc to new and printf to cout, by any stretch of the imagination.

2 of 3
32

I think the reason why it was originally written in ANSI C89 is quite simply because back then, C++ was just not a workable choice what with incompatibilities between different compilers and such. I mean, it took until, what was it, 2005, to come up with an ABI specification that would allow code compiled with one compiler to call code compiled with a different compiler?

The more interesting question is why it is still written in C89.

And there is a surprising answer: because people actually use Python on platforms for which no C++ and no C99 compiler exists! When the Forth-inspired threaded-code interpreter optimizations were merged, there was a huge discussion about it, because the code (necessarily) used computed goto which is not a part of C89. There were apparently real fears that this feature might not be available on some of the platforms that Python is currently used on.

The same thing happened with Unladen Swallow, wich uses LLVM, which is written in C++. It was made very clear that a requirement for merging Unladen Swallow into CPython would be that you can compile it without the JIT compiler, since there are platforms people run Python on, for which no C++ compiler exists.

Of course, nowadays, CPython is no longer the only Python implementation. There is PyPy, which is written in RPython (a statically typed subset of Python), Jython in Java, IronPython in C#, Pynie in NQP and PIR and so on.

🌐
GitHub
github.com › cython › cython
GitHub - cython/cython: The most widely used Python to C compiler · GitHub
The most widely used Python to C compiler. Contribute to cython/cython development by creating an account on GitHub.
Starred by 10.7K users
Forked by 1.6K users
Languages   Cython 51.5% | Python 41.9% | C 6.3% | C++ 0.2% | Shell 0.1% | Starlark 0.0%