PythonEngine
PythonEngine
#include <PythonEngine.hpp>
Public Functions
| Name | |
|---|---|
| PythonEngine() | |
| ~PythonEngine() | |
| PythonEngine(PythonEngine const & ) =delete | |
| PythonEngine & | operator=(PythonEngine const & ) =delete |
| PythonEngine(PythonEngine && ) =delete | |
| PythonEngine & | operator=(PythonEngine && ) =delete |
| PythonResult | execute(std::string const & code) Execute a string of Python code in the persistent namespace. |
| PythonResult | executeFile(std::filesystem::path const & path) Execute a .py file. |
| void | resetNamespace() Clear all user-defined variables from the namespace. |
| void | inject(std::string const & name, pybind11::object obj) Inject a pybind11 object into the Python namespace. |
| void | setWorkingDirectory(std::filesystem::path const & dir) const Set the Python working directory. |
| std::filesystem::path | getWorkingDirectory() const Get the current Python working directory. |
| void | setSysArgv(std::string const & args) const Set sys.argv for script execution. |
| PythonResult | executePrelude(std::string const & prelude) Execute a prelude code string. |
| std::string | validateVenv(std::filesystem::path const & venv_path) const Validate that a virtual environment is compatible. |
| std::string | activateVenv(std::filesystem::path const & venv_path) Activate a virtual environment. |
| void | deactivateVenv() Deactivate the currently active virtual environment. |
| std::filesystem::path | activeVenvPath() const Get the currently active venv path, or empty if none. |
| bool | isVenvActive() const Check whether a virtual environment is currently active. |
| std::vector< std::pair< std::string, std::string > > | listInstalledPackages() const List packages installed in the active venv. |
| PythonResult | installPackage(std::string const & package) Install a package in the active venv using pip. |
| std::pair< int, int > | pythonVersionTuple() const Get the Python version of the embedded interpreter (major, minor). |
| bool | isInitialized() const True once the interpreter has been successfully initialized. |
| pybind11::dict const & | globals() const Read-only access to the persistent globals dict. |
| pybind11::dict & | globals() Read-write access to the persistent globals dict (for PythonBridge). |
| std::vector< std::string > | getUserVariableNames() const Get a snapshot of user-defined variable names in the namespace. |
| std::string | pythonVersion() const The Python version string (e.g. “3.12.1”). |
| std::vector< std::filesystem::path > | discoverVenvs(std::vector< std::filesystem::path > const & extra_paths ={}) Discover virtual environments in common locations. |
| std::filesystem::path | executableDirectory() Directory containing the running executable. |
Public Functions Documentation
function PythonEngine
PythonEngine()function ~PythonEngine
~PythonEngine()function PythonEngine
PythonEngine(
PythonEngine const &
) =deletefunction operator=
PythonEngine & operator=(
PythonEngine const &
) =deletefunction PythonEngine
PythonEngine(
PythonEngine &&
) =deletefunction operator=
PythonEngine & operator=(
PythonEngine &&
) =deletefunction execute
PythonResult execute(
std::string const & code
)Execute a string of Python code in the persistent namespace.
Variables defined in one call remain available in subsequent calls (REPL-style). stdout and stderr are captured in the returned PythonResult.
function executeFile
PythonResult executeFile(
std::filesystem::path const & path
)Execute a .py file.
The file is read and executed via exec() in the persistent namespace. The working directory is temporarily changed to the script’s parent directory for the duration of execution.
function resetNamespace
void resetNamespace()Clear all user-defined variables from the namespace.
Re-imports the default prelude (builtins, etc.) so the interpreter remains usable.
function inject
void inject(
std::string const & name,
pybind11::object obj
)Inject a pybind11 object into the Python namespace.
After injection the object is accessible by name in subsequent execute() calls.
function setWorkingDirectory
void setWorkingDirectory(
std::filesystem::path const & dir
) constSet the Python working directory.
Parameters:
- dir Absolute path to the directory.
Calls os.chdir() to change to the specified directory. If the directory does not exist, this is a no-op.
function getWorkingDirectory
std::filesystem::path getWorkingDirectory() constGet the current Python working directory.
function setSysArgv
void setSysArgv(
std::string const & args
) constSet sys.argv for script execution.
Parameters:
- args Space-separated argument string.
Parses an argument string into a list of strings (splitting on whitespace, respecting quoted strings) and sets sys.argv.
function executePrelude
PythonResult executePrelude(
std::string const & prelude
)Execute a prelude code string.
Parameters:
- prelude Python code to execute as startup prelude.
Silently executes the given code (e.g. auto-imports). Errors are suppressed — the prelude is best-effort. Returns the PythonResult for diagnostic purposes.
function validateVenv
std::string validateVenv(
std::filesystem::path const & venv_path
) constValidate that a virtual environment is compatible.
Parameters:
- venv_path Root directory of the venv.
Return: Empty string on success, or a human-readable error message.
Checks:
- The path exists and looks like a venv (pyvenv.cfg or bin/python)
- The venv’s Python version matches the embedded interpreter’s major.minor
function activateVenv
std::string activateVenv(
std::filesystem::path const & venv_path
)Activate a virtual environment.
Parameters:
- venv_path Root directory of the venv.
Return: Empty string on success, or a human-readable error message.
Prepends the venv’s site-packages to sys.path, sets sys.prefix, sys.exec_prefix, and os.environ["VIRTUAL_ENV"].
Call validateVenv() first to check compatibility.
function deactivateVenv
void deactivateVenv()Deactivate the currently active virtual environment.
Restores sys.path, sys.prefix, sys.exec_prefix, and removes VIRTUAL_ENV from os.environ.
function activeVenvPath
std::filesystem::path activeVenvPath() constGet the currently active venv path, or empty if none.
function isVenvActive
bool isVenvActive() constCheck whether a virtual environment is currently active.
function listInstalledPackages
std::vector< std::pair< std::string, std::string > > listInstalledPackages() constList packages installed in the active venv.
Return: Package list, or empty if no venv is active or listing fails.
Uses importlib.metadata.distributions() to enumerate packages. Returns (name, version) pairs.
function installPackage
PythonResult installPackage(
std::string const & package
)Install a package in the active venv using pip.
Parameters:
- package Package spec (e.g. “numpy”, “scipy>=1.10”).
Return: PythonResult with pip’s stdout/stderr.
Runs pip install <package> as a subprocess in the venv. Blocks until completion.
function pythonVersionTuple
std::pair< int, int > pythonVersionTuple() constGet the Python version of the embedded interpreter (major, minor).
Returns e.g. {3, 12}.
function isInitialized
bool isInitialized() constTrue once the interpreter has been successfully initialized.
function globals
pybind11::dict const & globals() constRead-only access to the persistent globals dict.
function globals
pybind11::dict & globals()Read-write access to the persistent globals dict (for PythonBridge).
function getUserVariableNames
std::vector< std::string > getUserVariableNames() constGet a snapshot of user-defined variable names in the namespace.
Excludes builtins / dunder names / modules — useful for UI namespace inspectors.
function pythonVersion
std::string pythonVersion() constThe Python version string (e.g. “3.12.1”).
function discoverVenvs
static std::vector< std::filesystem::path > discoverVenvs(
std::vector< std::filesystem::path > const & extra_paths ={}
)Discover virtual environments in common locations.
Parameters:
- extra_paths Additional directories to scan.
Return: List of discovered venv root paths.
Scans:
extra_paths(user-configured directories)- ~/.virtualenvs/
- ~/.conda/envs/
- ~/.pyenv/versions/
Only returns paths that look like valid Python venvs (contain a pyvenv.cfg or bin/python).
function executableDirectory
static std::filesystem::path executableDirectory()Directory containing the running executable.
Used to locate bundled resources (for example resources/python) in installed and build-tree layouts. Falls back to the current working directory when the executable path cannot be resolved.
Updated on 2026-11-13 at 20:25:40 +0000