how low-level bindings work in python
I already knew that Python is mostly written in C and that it links to many C libraries for improved performance. However, I never looked into the example of how this works under the hood.
Let’s use uuid as the most simple example. uuid.py has the following code block
# Import optional C extension at toplevel, to help disabling it when testing
try:
import _uuid
_generate_time_safe = getattr(_uuid, "generate_time_safe", None)
_has_stable_extractable_node = _uuid.has_stable_extractable_node
_UuidCreate = getattr(_uuid, "UuidCreate", None)
except ImportError:
_uuid = None
_generate_time_safe = None
_has_stable_extractable_node = False
_UuidCreate = None
import _uuid comes from _uuidmodule.c. During compilation, _uuidmodule.c has a ton of ifdefs to select a correct library based on your platform.
You can check library your python is using by running
import _uuid
print(_uuid.__file__)
Mine is
❯ python3
Python 3.13.7 (main, Aug 14 2025, 11:12:11) [Clang 17.0.0 (clang-1700.0.13.3)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>> import _uuid
... print(_uuid.__file__)
...
/opt/homebrew/Cellar/python@3.13/3.13.7/Frameworks/Python.framework/Versions/3.13/lib/python3.13/lib-dynload/_uuid.cpython-313-darwin.so
>>>
We could also talk about how uuid.py has fallbacks to existing binaries installed on the system, but I try to keep my TILs short.
I hope you also learned something new.