Write C++ inside Python strings, auto-generate pybind11 bindings, compile cross-platform, and inject symbols at runtime.
Project description
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).
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file py_cpp_pair-1.0.0.tar.gz.
File metadata
- Download URL: py_cpp_pair-1.0.0.tar.gz
- Upload date:
- Size: 21.4 kB
- Tags: Source
- Uploaded using Trusted Publishing? Yes
- Uploaded via: twine/6.1.0 CPython/3.13.12
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
001838a1aab490b01948654897c9cd16fb9c9e5b9041d603c2947baca3335b2d
|
|
| MD5 |
7199676ef5ca620502453d94fd650749
|
|
| BLAKE2b-256 |
d5f29cfadb95a412a9436bf34f14b4e28e55ec18a8df663012966e22c7863494
|
Provenance
The following attestation bundles were made for py_cpp_pair-1.0.0.tar.gz:
Publisher:
release.yml on LegedsDaD/py_cpp_pair
-
Statement:
-
Statement type:
https://in-toto.io/Statement/v1 -
Predicate type:
https://docs.pypi.org/attestations/publish/v1 -
Subject name:
py_cpp_pair-1.0.0.tar.gz -
Subject digest:
001838a1aab490b01948654897c9cd16fb9c9e5b9041d603c2947baca3335b2d - Sigstore transparency entry: 1516072207
- Sigstore integration time:
-
Permalink:
LegedsDaD/py_cpp_pair@a541f80cee61f78ebd144a9938f38f21d0030bd9 -
Branch / Tag:
refs/heads/main - Owner: https://github.com/LegedsDaD
-
Access:
public
-
Token Issuer:
https://token.actions.githubusercontent.com -
Runner Environment:
github-hosted -
Publication workflow:
release.yml@a541f80cee61f78ebd144a9938f38f21d0030bd9 -
Trigger Event:
workflow_dispatch
-
Statement type:
File details
Details for the file py_cpp_pair-1.0.0-py3-none-any.whl.
File metadata
- Download URL: py_cpp_pair-1.0.0-py3-none-any.whl
- Upload date:
- Size: 21.5 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? Yes
- Uploaded via: twine/6.1.0 CPython/3.13.12
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
4bc03ebbca60e322f7321bf5a02e5f309b75e93b7c8e9e8d879c77ebbba1d340
|
|
| MD5 |
c1a6ddb128f8a90d788970e1e34294aa
|
|
| BLAKE2b-256 |
96b8eb9076e6e0a6775a2beecfe6369cead8671f0a09f1ad537c9f70acacf76f
|
Provenance
The following attestation bundles were made for py_cpp_pair-1.0.0-py3-none-any.whl:
Publisher:
release.yml on LegedsDaD/py_cpp_pair
-
Statement:
-
Statement type:
https://in-toto.io/Statement/v1 -
Predicate type:
https://docs.pypi.org/attestations/publish/v1 -
Subject name:
py_cpp_pair-1.0.0-py3-none-any.whl -
Subject digest:
4bc03ebbca60e322f7321bf5a02e5f309b75e93b7c8e9e8d879c77ebbba1d340 - Sigstore transparency entry: 1516072429
- Sigstore integration time:
-
Permalink:
LegedsDaD/py_cpp_pair@a541f80cee61f78ebd144a9938f38f21d0030bd9 -
Branch / Tag:
refs/heads/main - Owner: https://github.com/LegedsDaD
-
Access:
public
-
Token Issuer:
https://token.actions.githubusercontent.com -
Runner Environment:
github-hosted -
Publication workflow:
release.yml@a541f80cee61f78ebd144a9938f38f21d0030bd9 -
Trigger Event:
workflow_dispatch
-
Statement type: