py_cpp_pair
Write C++ directly inside Python strings, auto-generate pybind11 bindings, compile on-demand, dynamically load the extension, and inject exported symbols into your Python globals.
Version: 1.0.0
Badges
- PyPI:
py_cpp_pair 1.0.0(import name:py_cpp) - Python: 3.10–3.13
- Platforms: Windows / Linux / macOS
- CI: GitHub Actions
Install
pip install py_cpp_pair
Import name stays:
import py_cpp as pcp
Quickstart
import py_cpp as pcp
pcp.cpp("""
int add(int a, int b) {
return a + b;
}
""")
print(add(5, 7)) # 12
Class support
import py_cpp as pcp
pcp.cpp(r"""
class Robot {
public:
int power = 100;
Robot() {}
int attack() { return power * 2; }
};
""")
r = Robot()
print(r.attack()) # 200
Public API
Only these are intended as public:
pcp.cpp(code: str) -> modulepcp.install(package: str) -> boolpcp.clear_cache() -> Nonepcp.version() -> str
Cross-platform support
| OS | Compiler used | Notes |
|---|---|---|
| Windows | MinGW-w64 g++ (preferred) |
Requires g++ on PATH. |
| Linux | g++ |
Requires build tools installed. |
| macOS | clang++ |
Requires Xcode Command Line Tools. |
Override the compiler via:
# Windows (PowerShell)
$env:PY_CPP_CXX = "g++"
# Windows (cmd.exe)
set PY_CPP_CXX=g++
# Linux/macOS (bash/zsh)
export PY_CPP_CXX=clang++
Optional best-effort auto-install (opt-in):
# Enable by setting an environment variable in your shell.
# The installer logic is OS-aware (Linux/macOS/Windows) and picks the best available path.
# Windows (PowerShell)
$env:PY_CPP_AUTO_INSTALL_COMPILER = "1"
# Windows (cmd.exe)
set PY_CPP_AUTO_INSTALL_COMPILER=1
# Linux/macOS (bash/zsh)
export PY_CPP_AUTO_INSTALL_COMPILER=1
One-shot (only for a single command):
# Windows (PowerShell)
$env:PY_CPP_AUTO_INSTALL_COMPILER="1"; python -c "import py_cpp.toolchain as t; t.ensure_compiler_available(); print('compiler ok')"
# Linux/macOS (bash/zsh)
PY_CPP_AUTO_INSTALL_COMPILER=1 python -c "import py_cpp.toolchain as t; t.ensure_compiler_available(); print('compiler ok')"
Note: after an installer runs, you may need to open a new terminal so PATH updates are visible.
vcpkg package management
Install a C++ library with:
import py_cpp as pcp
pcp.install("fmt")
If the port does not exist:
Sorry, library not found.
py_cpp_pair detects vcpkg via VCPKG_ROOT or PATH. If missing, it can auto-bootstrap by cloning vcpkg (requires Git).
Caching and build layout
py_cpp_pair caches builds by SHA256 hash of the C++ source. Reusing identical code reuses the compiled extension.
Per-user state is stored under PY_CPP_HOME if set, otherwise the OS-default data directory:
- Windows:
%LOCALAPPDATA%\\py_cpp - macOS:
~/Library/Application Support/py_cpp - Linux:
$XDG_DATA_HOME/py_cppor~/.local/share/py_cpp
Subfolders:
builds/: compiled extensions per hashcache/: toolchain artifacts (Windows Python import libs)logs/: optional logs (enable withPY_CPP_LOG_FILE=1)
Troubleshooting
- ImportError: DLL load failed (Windows): ensure MinGW runtime DLLs are visible.
py_cpp_pairadds common DLL dirs automatically, but the compilerbindirectory must be onPATH. - No compiler found: install
g++/clang++and ensure it is onPATH. - vcpkg bootstrap fails: install Git and retry, or set
VCPKG_ROOTto an existing vcpkg checkout.
Performance notes
- Compilation is cached by source hash; repeated calls are fast after the first build.
- For larger projects, prefer fewer, bigger
cpp()calls (each call creates a module).
Documentation
See docs/:
docs/installation.mddocs/architecture.mddocs/api.mddocs/package_manager.mddocs/troubleshooting.mddocs/examples.md
Roadmap
- Better symbol detection (still regex-based by design)
- Multiple translation units / headers
- Better vcpkg library auto-linking
- Optional clang-based parsing for richer bindings
Contributing
See CONTRIBUTING.md.
Release (GitHub Actions)
This repo uses a single manual workflow: .github/workflows/release.yml.
- Mode =
check: buildssdist/wheeland runstwine check(no publishing). - Mode =
publish: runs the same checks, then creates a GitHub Release and publishes to PyPI using Trusted Publishing (OIDC).
Trusted Publishing setup (one-time):
- In your PyPI project settings, add this GitHub repo as a Trusted Publisher (no
PYPI_API_TOKENsecret needed).
Metadata
Release files for py-cpp-pair 1.0.0
For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.
Source distribution (sdist)
| File | Size | Uploaded | |
|---|---|---|---|
| py_cpp_pair-1.0.0.tar.gz | 21.4 kB | Details |
Built distribution (wheel)
| File | Interpreter | ABI | Platform | Reset |
|---|---|---|---|---|
| py_cpp_pair-1.0.0-py3-none-any.whl | Python 3 | none | any | Details |
Total release size: 42.9 kB
Release files / py_cpp_pair-1.0.0.tar.gz
| Download URL | py_cpp_pair-1.0.0.tar.gz |
|---|---|
| Size | 21.4 kB |
| Tags | Source |
|
SHA-256 checksum How to use checksums |
001838a1aab490b01948654897c9cd16fb9c9e5b9041d603c2947baca3335b2d
|
|
BLAKE2b-256 checksum How to use checksums |
d5f29cfadb95a412a9436bf34f14b4e28e55ec18a8df663012966e22c7863494
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
Yes |
| Uploaded via |
twine/6.1.0 CPython/3.13.12
|
Provenance
Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.
PyPI Publish Attestation
PyPI verified that this artifact, at this checksum, originated from the publisher listed below.
Signed by GitHub Actions, verified by PyPI on May 12, 2026.
Transparency logRelease files / py_cpp_pair-1.0.0-py3-none-any.whl
| Download URL | py_cpp_pair-1.0.0-py3-none-any.whl |
|---|---|
| Size | 21.5 kB |
| Tags | Python 3 |
|
SHA-256 checksum How to use checksums |
4bc03ebbca60e322f7321bf5a02e5f309b75e93b7c8e9e8d879c77ebbba1d340
|
|
BLAKE2b-256 checksum How to use checksums |
96b8eb9076e6e0a6775a2beecfe6369cead8671f0a09f1ad537c9f70acacf76f
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
Yes |
| Uploaded via |
twine/6.1.0 CPython/3.13.12
|
Provenance
Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.
PyPI Publish Attestation
PyPI verified that this artifact, at this checksum, originated from the publisher listed below.
Signed by GitHub Actions, verified by PyPI on May 12, 2026.
Transparency log