SIP - Python Bindings Generator
SIP is a tool for creating Python bindings for C and C++ libraries. It is primarily used to generate Python modules that allow Python code to call C/C++ functions and classes directly. Maintained by Riverbank Computing, it is the foundation for PyQt. The current version is 6.15.3, with releases typically tied to PyQt development.
Common errors
-
AttributeError: module 'sip' has no attribute 'setapi'
cause Attempting to call the `setapi` function which was removed in SIP v6.fixThis function is obsolete in SIP v6. API versioning is now implicitly managed by the `sip-build` tool during the binding generation process. Remove any `setapi` calls from your Python code or `.sip` files. -
ImportError: DLL load failed while importing sip
cause This error on Windows typically indicates a missing or incompatible C++ runtime library (e.g., Visual C++ Redistributable), an issue with the Python environment path, or a corrupted `sip` installation.fixInstall the latest Microsoft Visual C++ Redistributable for your system. Verify your Python installation's integrity. If in a virtual environment, try recreating it and reinstalling `sip`. For C/C++ projects, ensure they are compiled with compatible toolchains. -
RuntimeError: The PyQt5.sip API has not been set.
cause This error occurs specifically with PyQt5 when the underlying SIP API is not correctly configured or when attempting to use a SIP v6 module with a PyQt5 installation.fixEnsure you are using a version of `sip` compatible with PyQt5 (typically v4 or v5). PyQt6 does not use `setapi` and would not yield this error. Check your `pip freeze` output and consider isolating Python environments with `venv` or `conda`.
Warnings
- breaking SIP v6 introduced significant breaking changes from v5, including the removal of `sip.setapi()` and the standalone `sipconfig` module. The API versioning and configuration are now implicitly handled during the build process using the `sip-build` tool.
- gotcha Mixing SIP versions or using a `sip` module incompatible with a generated library (e.g., PyQt) can lead to `ImportError` or `RuntimeError` due to ABI incompatibilities. For example, PyQt5 typically uses SIP v4/v5, while PyQt6 requires SIP v6.
- gotcha When building C/C++ extensions with SIP, ensure your development environment has a compatible C++ compiler (e.g., MSVC on Windows, GCC/Clang on Linux/macOS) and that necessary build tools (like `make` or `ninja`) are correctly configured and accessible in your system's PATH.
Install
-
pip install sip
Imports
- sip module
from sip import setapi
import sip
- SIP API from PyQt
from PyQt6 import sip
Quickstart
import sip
# SIP is primarily a C/C++ bindings generator, not typically used directly in applications.
# This example shows basic introspection of the installed sip module.
print(f"SIP Version: {sip.SIP_VERSION_STR}")
print(f"SIP API ID: {sip.SIP_API_ID_STR}")
# To truly 'quickstart' SIP for generating bindings, you would:
# 1. Write a .sip file defining the C/C++ API.
# 2. Use the 'sip-build' command-line tool or integrate it into your build system.
# Example (conceptual command-line use):
# # In a terminal, after setting up project.sip and C/C++ sources:
# # sip-build --build-dir=build --sip-module=my_module project.sip