Registry / devops / swig
library4.5.0pypypi✓ verified 23d ago

SWIG is a software development tool that facilitates connecting programs written in C and C++ with a variety of high-level programming languages, including Python. It works by generating 'wrapper' code that allows scripting languages to access underlying C/C++ code. The current version is 4.4.1, with releases focused on C/C++ standard improvements, expanded language support, and Python compatibility updates.

pip install swig
INSTALL
IMPORT
SIG · SWIG
S
swig
devopspythonv4.5.0
Install
1.8s avg
Import
20ms
Disk
26MB
Pass rate
10/ 10
Env Coverage10 / 10
glibc
3.93.13
musl
3.93.13
Install & Compatibility
Where this runs
tested against v4.5.0 · pip install
no network on importno background threads
Install × environment matrix
Each cell = how many times install + import succeeded across repeated harness runs. Partial = flaky.
glibc = Debian/Ubuntu slim · musl = Alpine Linux
musl
py 3.103.95 runs
installs and imports cleanly · install 0.0s · import 0.022s · 29.7MB
glibc
py 3.103.95 runs
installs and imports cleanly · install 1.8s · import 0.018s · 26MB
26MB installed
● package 26MB
Code
Verified usage

Verified import paths — ran on the pinned version, not inferred.

swig
import swig
import <module_name>

This quickstart demonstrates the full SWIG workflow: defining C/C++ functions, creating a SWIG interface file (`.i`), using the `swig` command to generate Python wrapper code and a Python module file, compiling the C/C++ source with the generated wrapper, linking them into a shared library, and finally importing and using the module in Python. Note that the compilation commands (g++) are platform-dependent and assume a C++ compiler and Python development headers are installed.

/* C/C++ file: example.h */ #ifndef EXAMPLE_H #define EXAMPLE_H int fact(int n); int my_mod(int x, int y); #endif // EXAMPLE_H /* C/C++ file: example.c */ #include "example.h" int fact(int n) { if (n < 0) return 0; /* Or handle error appropriately */ if (n == 0) return 1; return n * fact(n - 1); } int my_mod(int x, int y) { if (y == 0) return 0; /* Or handle error appropriately */ return x % y; } /* SWIG interface file: example.i */ %module example %{ #define SWIG_FILE_WITH_INIT #include "example.h" %} int fact(int n); int my_mod(int x, int y); # Build and run steps in bash # 1. Generate SWIG wrapper code # swig -python -c++ example.i # 2. Compile C/C++ source and SWIG wrapper # g++ -c example.c example_wrap.cxx -I/usr/include/python3.10 -fPIC # 3. Link into a shared library # g++ -shared example.o example_wrap.o -o _example.so # Python usage: import example print(f"Factorial of 5: {example.fact(5)}") print(f"7 mod 3: {example.my_mod(7, 3)}")
swig --version
Debug
Known issues
breakingSWIG 4.4.0 changed the default generation of static types to heap types and introduced multi-phase module initialization for modern Python wrappers. This might require adjustments in older build systems or specific configurations, especially for projects relying on specific Python C API internals.
fix
Review your build system and generated wrapper code for compatibility with heap types and multi-phase initialization. If using `Py_LIMITED_API`, ensure compatibility, as fixes for combinations with recent Python versions were added in 4.4.1.
affects: 4.4.0+
gotchaWhen importing multiple SWIG-generated modules using `from <module> import *`, you might encounter a name clash on the special `cvar` object used to access global variables. This means only the `cvar` from the last loaded module will be accessible directly.
fix
Avoid `from <module> import *` when dealing with multiple SWIG modules. Instead, use `import <module_name>` and access global variables via `<module_name>.cvar.<variable_name>`.
affects: All versions
gotchaThe generated Python shared library filename must precisely match the module name specified in the SWIG interface file (`%module <module_name>`), typically with a leading underscore (e.g., `_example.so` for `%module example`). A mismatch will lead to import errors.
fix
Ensure your compilation/linking step creates a shared library named `_<module_name>.so` (or `_<module_name>.pyd` on Windows) that matches the `%module` directive in your `.i` file.
affects: All versions
gotchaMemory leaks can occur when SWIG wraps C/C++ pointers to opaque types (e.g., `HWND*` on Windows) and doesn't find a corresponding destructor. SWIG treats them as raw pointers without proper memory management.
fix
Provide explicit definitions for opaque types (e.g., `typedef void* HWND;` in the interface file) or use custom typemaps to instruct SWIG on how to manage the memory for such pointers, for instance, by treating handles as integers.
affects: All versions
gotchaIssues can arise when C++ callbacks pass references to parameters that are then accessed in Python. The underlying C++ type might not correctly align with the Python instance, leading to errors when attempting to access methods of the referenced object in Python.
fix
Carefully ensure that C++ namespaces are correctly set in your SWIG interface file, especially when dealing with classes and callbacks that pass parameters by reference. This helps SWIG correctly map the C++ types to their Python representations.
affects: All versions
Upgrade
Version history
4.5.0latest on PyPI · released Aug 21, 2026
Audit
Dependencies

No dependency data recorded yet.

Agent activity
13 hits · last 30 days
node
10
Resources
swig — pip install swig · libregistry