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

The pythonEnv class controls the Python environment used for external and in-process LayoutScript execution. By default a virtual environment is kept under ~/LayoutEditor/python_env. Packages can be installed with pip without root privileges.

If the LayoutEditor was started inside an already activated virtual environment (VIRTUAL_ENV is set), that environment is used instead and ~/LayoutEditor/python_env is not created.

LayoutScript and _LayoutScript are shipped with the LayoutEditor installation. They must not be installed with pip.

This class was introduced with release 20260718.

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. Returns: the process exit code. Use this to call Python CLI tools (for example later volare). (introduced with release 20260718)

#!/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)