Manages the user-local Python virtual environment for LayoutScript. More...
| 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() |
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.
Returns: the active environment root (VIRTUAL_ENV or ~/LayoutEditor/python_env). (introduced with release 20260718)
Returns: the Python executable inside the active environment. (introduced with release 20260718)
Returns: the site-packages directory of the active environment. (introduced with release 20260718)
Returns: true if the LayoutEditor was started with VIRTUAL_ENV set. (introduced with release 20260718)
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;
}
Returns: true if the pip package name is installed in the active environment. (introduced with release 20260718)
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;
}
Uninstalls package with pip. Returns: the process exit code. (introduced with release 20260718)
Returns: the output of pip list. (introduced with release 20260718)
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;
}
Returns: stdout/stderr of the last pip / venv / python -m invocation. (introduced with release 20260718)