Building and Installing the KaHyPar Python Module from Source

January 2026

← Back to other guides

This guide shows you how to build KaHyPar’s Python bindings from source and manually installing the resulting shared library into Python’s site-packages. It works on an Apple M4 Pro chip with Python 3.12.8.

1. Clone the KaHyPar repository

Clone the repository with required submodules.

git clone --depth=1 --recursive https://github.com/kahypar/kahypar.git

Move into the repository directory.

cd kahypar

2. Create and enter the build directory

KaHyPar uses an out-of-tree build layout.

mkdir -p build
cd build

3. Build the Python bindings

Enter the Python build directory and compile the module.

cd python
make

4. Locate the compiled shared library

After building, a shared object (.so) file should be present.

ls -lh *.so
Note: The filename may differ depending on your Python version (for example, it may include cpython-312 in the name). Use the exact .so filename you see here in the copy step below.

5. Find your Python site-packages directory

Determine where Python installs packages.

SITEPKG=$(python -c "import site; print(site.getsitepackages()[0])")

6. Install the module manually

Copy the compiled shared library into site-packages.

cp kahypar*.so "$SITEPKG/kahypar.so"
Note: This copies whatever kahypar*.so you built into site-packages under the stable name kahypar.so, so import kahypar works consistently. If multiple matches exist, copy the specific file name instead of using the wildcard.

7. Return to your original directory

cd ../../../

8. Verify the installation

Check that Python can successfully import KaHyPar.

python -c 'import kahypar; print("Success! Attributes:", [x for x in dir(kahypar) if not x.startswith("_")])'
Common pitfalls: