Class Reference pythonEnv

Manages the user-local Python virtual environment for LayoutScript. More...

Member

string path()
string pythonExecutable()
string sitePackagesPath()
bool isExternallyActivated()
bool ensure()
bool isPackageInstalled(string name)
int pipInstall(string package)
int pipUninstall(string package)
string pipList()
int runModule(string module,stringList args=stringList())
string lastOutput()

Detailed Description

pythonEnv is a static hub for the Python virtual environment used by external and in-process LayoutScript. There is no object to construct: call pythonEnv::ensure, pythonEnv::path, or pythonEnv::pipInstall. By default the environment is ~/LayoutEditor/python_env. If the LayoutEditor was started with VIRTUAL_ENV already set, that environment is used instead and the managed folder is not created. pip installs packages there without root; LayoutScript and _LayoutScript ship with the editor and must not be installed with pip. runModule runs python -m in that environment (for example later volare). lastOutput holds stdout and stderr of the last pip, venv, or python -m call. This class was introduced with release 20260718.

Example:

if (!pythonEnv::ensure()) {
 layout->showMessage("pythonEnv", pythonEnv::lastOutput());
 return 1;
}
layout->showStatus("Python env: " + pythonEnv::path());

Member Function Documentation


static string pythonEnv::path()

Returns: the active environment root (VIRTUAL_ENV or ~/LayoutEditor/python_env). (introduced with release 20260718)


static string pythonEnv::pythonExecutable()

Returns: the Python executable inside the active environment. (introduced with release 20260718)


static string pythonEnv::sitePackagesPath()

Returns: the site-packages directory of the active environment. (introduced with release 20260718)


static bool pythonEnv::isExternallyActivated()

Returns: true if the LayoutEditor was started with VIRTUAL_ENV set. (introduced with release 20260718)


static bool pythonEnv::ensure()

Creates the managed virtual environment under ~/LayoutEditor/python_env if it does not exist yet. Does nothing when an external virtual environment is already active. Returns: true on success. (introduced with release 20260718)

#!/usr/bin/layout
#name=ensure python env
#help=create ~/LayoutEditor/python_env if needed

int main(){
 if (pythonEnv::ensure()) {
 layout->showStatus("Python env: "+pythonEnv::path());
 } else {
 layout->showMessage("pythonEnv",pythonEnv::lastOutput());
 }
 return 0;
}

static bool pythonEnv::isPackageInstalled(string name)

Returns: true if the pip package name is installed in the active environment. (introduced with release 20260718)


static int pythonEnv::pipInstall(string package)

Installs package with pip into the active environment. Returns: the process exit code (0 on success). Refuses LayoutScript / _LayoutScript (shipped with LayoutEditor, not via PyPI). (introduced with release 20260718)

#!/usr/bin/layout
#name=pip install example
#help=install a Python package into the LayoutEditor env

int main(){
 pythonEnv::ensure();
 if (pythonEnv::pipInstall("requests")!=0) {
 layout->showMessage("pipInstall failed",pythonEnv::lastOutput());
 }
 return 0;
}

static int pythonEnv::pipUninstall(string package)

Uninstalls package with pip. Returns: the process exit code. (introduced with release 20260718)


static string pythonEnv::pipList()

Returns: the output of pip list. (introduced with release 20260718)


static int pythonEnv::runModule(string module,stringList args=stringList())

Runs python -m module with args in the active environment. Use this to call Python CLI tools (for example later volare). (introduced with release 20260718)

Parameter:
module Python module name passed to python -m
args extra arguments for that module

Returns: the process exit code.

#!/usr/bin/layout
#name=run python module
#help=example for pythonEnv::runModule

int main(){
 pythonEnv::ensure();
 stringList args;
 // args.append("--help");
 int code=pythonEnv::runModule("pip",args);
 layout->showMessage("runModule",pythonEnv::lastOutput());
 return code;
}

static string pythonEnv::lastOutput()

Returns: stdout/stderr of the last pip / venv / python -m invocation. (introduced with release 20260718)