find-libpython
find-libpython is a Python library and script designed to robustly locate the `libpython` dynamic shared library associated with the current Python environment. It supports various Python installations, including conda-managed, system-managed, and virtual environments, across Windows, macOS, and Linux. This utility is crucial for projects embedding a Python interpreter or for Python library build systems that need to link against `libpython`. The current version is 0.5.1, released on February 10, 2026, with an active, though irregular, release cadence.
Warnings
- gotcha Python installations (especially custom builds or newer Linux defaults) may produce a *static* `libpython.a` instead of a *shared* `libpython.so` or `.dylib`. `find-libpython` is designed to locate *shared* libraries. If `find_libpython()` returns `None`, it often indicates that the Python interpreter was compiled without `--enable-shared` or similar flags, making `libpython` unavailable for dynamic linking.
- gotcha Relying solely on environment variables like `LD_LIBRARY_PATH` (Linux) or `DYLD_LIBRARY_PATH` (macOS) to make `libpython` discoverable can be brittle and inconsistent, especially in complex deployment scenarios or when embedding Python. While these might offer temporary fixes, system-wide linker configuration (e.g., `ldconfig` on Linux, or correct RPATHs during compilation) provides a more robust solution.
- gotcha In environments with multiple Python installations (e.g., system Python, Conda, Pyenv, virtual environments), `find-libpython` attempts to find the `libpython` associated with the *currently active* Python interpreter. Issues can arise if the desired Python environment is not properly activated or if the `PATH` variable leads to an unexpected interpreter, causing `find_libpython()` to return an incorrect path or `None`.
Install
-
pip install find-libpython
Imports
- find_libpython
from find_libpython import find_libpython
Quickstart
from find_libpython import find_libpython
lib_path = find_libpython()
if lib_path:
print(f"Found libpython at: {lib_path}")
else:
print("Could not find libpython for the current environment.")