๐ŸŒ
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 ... 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....
๐ŸŒ
Llllllllll
llllllllll.github.io โ€บ c-extension-tutorial โ€บ what-is-an-extension-module.html
What is an Extension Module? โ€” c-extension-tutorial documentation
A CPython extension module is a module which can be imported and used from within Python which is written in another language.
๐ŸŒ
Python Developer's Guide
devguide.python.org โ€บ developer-workflow โ€บ extension-modules
Standard library extension modules - Python Developer's Guide
May 24, 2026 - By convention, the source file containing the extension module implementation is called <NAME>module.c, where <NAME> is the name of the module that will be later imported (in our case _foo). In addition, the directory containing the implementation should also be named similarly. ... #ifndef _FOO_HELPER_H #define _FOO_HELPER_H #include "Python.h" typedef struct { /* ...
๐ŸŒ
Python
docs.python.org โ€บ 3 โ€บ c-api โ€บ extension-modules.html
Defining extension modules โ€” Python 3.14.7 documentation
A C extension for CPython is a shared library (for example, a .so file on Linux, .pyd DLL on Windows), which is loadable into the Python process (for example, it is compiled with compatible compiler settings), and which exports an initialization function. To be importable by default (that is, by importlib.machinery.ExtensionFileLoader), the shared library must be available on sys.path, and must be named after the module name plus an extension listed in importlib.machinery.EXTENSION_SUFFIXES.
๐ŸŒ
Tutorialspoint
tutorialspoint.com โ€บ python โ€บ python_further_extensions.htm
Python - Further Extensions
A Python extension module is nothing more than a normal C library. On Unix machines, these libraries usually end in .so (for shared object).
๐ŸŒ
Real Python
realpython.com โ€บ build-python-c-extension-module
Building a Python C Extension Module โ€“ Real Python
March 18, 2026 - In this tutorial, you'll learn how to write Python interfaces in C. Find out how to invoke C functions from within Python and build Python C extension modules. You'll learn how to parse arguments, return values, and raise custom exceptions using the Python API.
๐ŸŒ
Reddit
reddit.com โ€บ r/learnpython โ€บ what is a python extension
r/learnpython on Reddit: What is a Python Extension
January 13, 2025 -

I have heard a lot of Python extensions, extension modules, and Windows extensions. But qualifies something as an extension to Python. What criteria is to be met before for a module or library is said to be an extension to Python.

If a library or module uses shared libraries from ( ie. C, C++ ) and provides features to Python whether already provided by Python or not, does the module become an extension to Python ?

Or the module must integrate with the Python runtime and provide features not immediately provided by Python to be called an extension.

This is part of a larger quest to have a good understanding of Python and it workings.

๐ŸŒ
Python
docs.python.org โ€บ 3 โ€บ extending โ€บ building.html
4. Building C and C++ Extensions โ€” Python 3.14.7 documentation
A C extension for CPython is a shared library (for example, a .so file on Linux, .pyd on Windows), which exports an initialization function.
๐ŸŒ
Opensource.com
opensource.com โ€บ article โ€บ 22 โ€บ 11 โ€บ extend-c-python
Write a C++ extension module for Python | Opensource.com
November 24, 2022 - GDB invokes the CPython interpreter with the script file main.py. The script file allows you to easily define all the actions you want to perform with the Python extension module.
Find elsewhere
๐ŸŒ
Thomasnyberg
thomasnyberg.com โ€บ what_are_extension_modules.html
What are (c)python extension modules? - Thomas Nyberg
November 12, 2017 - Next we need to actually build this module in way that python can import. This is most easily done using the setuptools module. The following is a pretty minimal build script: ... import os from setuptools import setup, Extension module = Extension('spam', sources=['spammodule.c']) setup(name='spam', ext_modules = [module])
๐ŸŒ
Readthedocs
pyoxidizer.readthedocs.io โ€บ en โ€บ v0.9.0 โ€บ packaging_extension_modules.html
Working with Python Extension Modules โ€” PyOxidizer 0.9.0 documentation
Typically, built-in extension modules only exist in Python distributions (and are part of the Python standard library by definition) and Python package maintainers only ever produce standalone extension modules (e.g.
๐ŸŒ
PyPI
pypi.org โ€บ project โ€บ extensions
extensions ยท PyPI
from extensions import register # usage : register(group, name, location) register('myapp.operator', 'average', 'myapp.extensions:average') The third parameter gives the location of the callable, with the form module_name:attrs, where module_name is the full name of the module, and attrs the attributes in the module.
Top answer
1 of 6
36

A few ways.

The easy way:

Don't extend the module, extend the classes.

exttwitter.py

import twitter

class Api(twitter.Api):
    pass 
    # override/add any functions here.

Downside : Every class in twitter must be in exttwitter.py, even if it's just a stub (as above)

A harder (possibly un-pythonic) way:

Import * from python-twitter into a module that you then extend.

For instance :

basemodule.py

 class Ball():
    def __init__(self,a):
        self.a=a
    def __repr__(self):
        return "Ball(%s)" % self.a

def makeBall(a):
    return Ball(a)

def override():
    print "OVERRIDE ONE"

def dontoverride():
    print "THIS WILL BE PRESERVED"

extmodule.py

from basemodule import *
import basemodule

def makeBalls(a,b):
    foo = makeBall(a)
    bar = makeBall(b)
    print foo,bar

def override():
    print "OVERRIDE TWO"

def dontoverride():
    basemodule.dontoverride()
    print "THIS WAS PRESERVED"

runscript.py

import extmodule

#code is in extended module
print extmodule.makeBalls(1,2)
#returns Ball(1) Ball(2)

#code is in base module
print extmodule.makeBall(1)
#returns Ball(1)

#function from extended module overwrites base module
extmodule.override()
#returns OVERRIDE TWO

#function from extended module calls base module first
extmodule.dontoverride()
#returns THIS WILL BE PRESERVED\nTHIS WAS PRESERVED

I'm not sure if the double import in extmodule.py is pythonic - you could remove it, but then you don't handle the usecase of wanting to extend a function that was in the namespace of basemodule.

As far as extended classes, just create a new API(basemodule.API) class to extend the Twitter API module.

2 of 6
7

Don't add them to the module. Subclass the classes you want to extend and use your subclasses in your own module, not changing the original stuff at all.

๐ŸŒ
Python documentation
docs.python.org โ€บ 3 โ€บ howto โ€บ isolating-extensions.html
Isolating Extension Modules โ€” Python 3.14.7 documentation
Abstract: Traditionally, state belonging to Python extension modules was kept in C static variables, which have process-wide scope. This document describes problems of such per-process state and sh...
๐ŸŒ
Medium
medium.com โ€บ @kuldeepyadav7291 โ€บ python-extension-modules-5e07bcbb85bf
Python Extension Modules. Disclaimer: I am still exploring thisโ€ฆ | by Kuldeep Yadav | Medium
July 16, 2023 - Here, one thing to note that the function name should always follow naming standard PyInit_* and * should match with your module name defined in module definition. Now, you can write the sort implementation in function wildsort_sort method or can delegate it to any external shared library, which will involve some nitty gritties of converting python objects from and back to data structures in C.
๐ŸŒ
GeeksforGeeks
geeksforgeeks.org โ€บ c-extension-module-using-python
C Extension Module using Python | GeeksforGeeks
March 27, 2019 - Writing a simple C extension module directly using Pythonโ€™s extension API and no other tools. It is straightforward to make a handcrafted extension module for a simple C code. But first, we have to make sure that the C code has a proper header file.
๐ŸŒ
O'Reilly
oreilly.com โ€บ library โ€บ view โ€บ python-in-a โ€บ 0596100469 โ€บ ch01s02.html
The Python Standard Library and Extension Modules - Python in a Nutshell, 2nd Edition [Book]
July 14, 2006 - Extension modules, from the standard library or from elsewhere, let Python code access functionality supplied by the underlying operating system or other software components such as graphical user interfaces (GUIs), databases, and networks.
Author: Alex Martelli
Published: 2006
Pages: 734
๐ŸŒ
Cornell Virtual Workshop
cvw.cac.cornell.edu โ€บ python-performance โ€บ compiling-code โ€บ extension-modules
Cornell Virtual Workshop > Python for High Performance > Compiling Custom Code > Extension Modules
The Python/C API defines a process ... called from C). Extension modules are built from compiled code, communicating with Python through the C API, and which can be imported into Python in the same manner as a module written in pure Python....
๐ŸŒ
Setuptools
setuptools.pypa.io โ€บ en โ€บ stable โ€บ userguide โ€บ ext_modules.html
Building Extension Modules - setuptools 82.0.1 documentation
This means that all source files will be compiled into a single binary file <module path>.<suffix> (with <module path> derived from name and <suffix> defined by one of the values in importlib.machinery.EXTENSION_SUFFIXES). In the case .pyx files are passed as sources and Cython is not installed in the build environment, setuptools may also try to look for the equivalent .cpp or .c files. ... name (str) โ€“ the full name of the extension, including any packages โ€“ ie. not a filename or pathname, but Python dotted name