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() |
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());
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. 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;
}
Returns: stdout/stderr of the last pip / venv / python -m invocation. (introduced with release 20260718)