Python ctypes: call functions in shared libraries
Answer: ctypes is a foreign function library for Python. It supplies C compatible data types and can call functions in a DLL or shared library. It can be used to wrap these libraries in pure Python.
Examples
import ctypes
libc = ctypes.CDLL("libc.so.6")
Notes
Some samples reference the c_int type. Where sizeof(long) equals sizeof(int), that type is an alias to c_long. Follow the tutorial in the library docs when you wrap a shared library.
Reference list
ctypesis a foreign function library for Python.providesIt provides C compatible data types and can call functions in DLLs.Youcan wrap those libraries in pure Python.
See also
Claims
- id: C1 claim: ctypes is a foreign function library for Python. source: S2 evidence: "ctypes` is a foreign function library for Python"
- id: C2 claim: It provides C compatible data types and can call functions in DLLs. source: S1 evidence: "It provides C compatible data types, and allows calling functions in DLLs or shared libraries"
- id: C3 claim: You can wrap those libraries in pure Python. source: S2 evidence: "It can be used to wrap these libraries in pure Python"
Sources
- [S1] ctypes — A foreign function library for Python. Python docs. https://docs.python.org/3/library/ctypes.html . License: PSF-2.0. Accessed 2026-11-18.
- [S2] ctypes.rst. CPython docs. https://raw.githubusercontent.com/python/cpython/main/Doc/library/ctypes.rst . License: PSF-2.0. Accessed 2026-11-18.
License: this note is CC BY 4.0 (https://creativecommons.org/licenses/by/4.0/). Summary by iinventedlight (AI-assisted). Sources: Python docs, ctypes — A foreign function library for Python; CPython docs, ctypes.rst. PSF-2.0 (https://docs.python.org/3/license.html). Copyright Python Software Foundation. Text was condensed and changed from the originals.
License: CC-BY-4.0 · Summary by iinventedlight (AI-assisted). Sources: Python docs, ctypes — A foreign function library for Python; CPython docs, ctypes.rst. PSF-2.0 (https://docs.python.org/3/license.html). Copyright Python Software Foundation.
Cite this
iinventedlight. 2026. "Python ctypes: call functions in shared libraries." iinventedlight Library, iil:note:d4zczrrg, version of 2026-11-18. https://iinventedlight.com/library/notes/python-ctypes/. License: CC-BY-4.0.
@misc{iil_note_d4zczrrg,
author = {{iinventedlight}},
title = {{Python ctypes: call functions in shared libraries}},
howpublished = {iinventedlight Library},
year = {2026},
date = {2026-11-18},
url = {https://iinventedlight.com/library/notes/python-ctypes/},
note = {Permanent ID iil:note:d4zczrrg. License: CC-BY-4.0.},
}
Spotted a mistake? Use the MCP feedback tool or email hello@iinventedlight.com. Or Report a correction (no account needed).
Extra files beside this note
These files use this note's license, shown below. Paper extras retain their reproduction status; a link is not a reproduction.
- python-ctypes.table.json (table; CC-BY-4.0)
- python-ctypes.schema.json (schema; CC-BY-4.0)
For language models and agents: start at /llms.txt | /agents.md | /.well-known/agent-library.json. This page in Markdown: /library/notes/python-ctypes.md.