pkgconfig
pkgconfig is a Python library that provides a simple interface to the `pkg-config` command-line tool. It allows Python applications to query installed libraries for their build flags (Cflags, Libs) and version information, making it easier to build and link against external C/C++ libraries. The current version is 1.6.0, and it has an infrequent release cadence, typically releasing new versions only when significant updates or fixes are needed.
Warnings
- gotcha The Python `pkgconfig` library is a wrapper around the system's `pkg-config` command-line tool. It requires the `pkg-config` executable to be installed and available in the system's PATH. This library does not install `pkg-config` itself.
- gotcha This library only reads existing `.pc` files that `pkg-config` knows about. It does not provide functionality to install new `.pc` files or manage `pkg-config` package definitions. Its purpose is solely to query system-level `pkg-config` data.
- deprecated The `pkgconfig.VARIABLES` attribute was removed in version 1.4.0. To access package variables, use the `pkgconfig.parse()` function and retrieve them from the returned dictionary via the `'Variables'` key (e.g., `pkgconfig.parse('foobar')['Variables']`).
Install
-
pip install pkgconfig
Imports
- pkgconfig
import pkgconfig
Quickstart
import pkgconfig
# Check if a package exists
if pkgconfig.exists('glib-2.0'):
print('glib-2.0 is installed.')
# Get CFLAGS for compilation
cflags = pkgconfig.cflags('glib-2.0')
print(f'CFlags: {cflags}')
# Get LIBS for linking
libs = pkgconfig.libs('glib-2.0')
print(f'Libs: {libs}')
# Get the package version
version = pkgconfig.modversion('glib-2.0')
print(f'Version: {version}')
# Parse all information into a dictionary
parsed_info = pkgconfig.parse('glib-2.0')
print(f'Parsed Info: {parsed_info}')
else:
print('glib-2.0 is not installed or not found by pkg-config.')
# Example with a non-existent package to show handling
if not pkgconfig.exists('nonexistent-library-xyz'):
print('nonexistent-library-xyz is not found, as expected.')