Pact Python FFI
`pact-python-ffi` provides low-level Python bindings for the underlying Pact FFI (Foreign Function Interface) library, which is written in Rust. It enables Python applications to directly interact with the core Pact logic for contract testing. While often an internal dependency of the higher-level `pact-python` library, it can be used independently for advanced FFI control. The current version is `0.4.28.2`, and it is part of the `pact-python` monorepo, following its active release cadence.
Common errors
-
ModuleNotFoundError: No module named 'pact.v3.ffi'
cause You are attempting to import from the deprecated `pact.v3.ffi` module which was removed in recent versions of `pact-python-ffi`.fixChange your import statements from `from pact.v3.ffi import ...` to `from pact_ffi import ...`. -
ERROR: Package 'pact-python-ffi' requires a different Python version: 3.10 or newer, but you have 3.9.x (or similar pip installation error)
cause You are attempting to install `pact-python-ffi` on an unsupported Python version (Python 3.9 or older).fixUpgrade your Python environment to version 3.10 or newer. You can use tools like `pyenv` or `conda` to manage Python versions.
Warnings
- breaking The `pact.v3.ffi` module has been removed and its functions replaced by the top-level `pact_ffi` package.
- breaking Python 3.9 support has been officially dropped.
- gotcha `pact-python-ffi` is a low-level FFI binding. Most users seeking Python-based Pact contract testing should primarily use the `pact-python` library, which leverages `pact-python-ffi` internally, rather than interacting directly with `pact-python-ffi`.
Install
-
pip install pact-python-ffi
Imports
- ffi_version
from pact.v3.ffi import ffi_version
from pact_ffi import ffi_version
- ffi_init
from pact_ffi import ffi_init
- ffi_log_level
from pact_ffi import ffi_log_level
Quickstart
import os
from pact_ffi import ffi_version, ffi_log_level, ffi_init
# Initialize the FFI logging
# PACT_LOG_LEVEL can be 'trace', 'debug', 'info', 'warn', 'error'
log_level = os.environ.get('PACT_LOG_LEVEL', 'info')
ffi_init(log_level)
print(f"Pact FFI initialized with log level: {log_level}")
# Get and print the FFI library version
version = ffi_version()
print(f"Pact FFI library version: {version}")
# Example of setting log level dynamically (optional, usually set once)
# ffi_log_level("debug")
# print("Log level set to debug.")