Node python-environment
Configuration node of @rosepetal/node-red-contrib-python-executor. It creates a Python virtual environment on the machine that runs Node-RED and installs and uninstalls its packages with pip, from the editor. A python-executor node that selects it in its Environment field runs its code with the interpreter of that environment.
The node stores only a name. The environment is a directory on disk, created and changed by the buttons of the edit dialog at the moment they are pressed, not by a deploy.
Requirements
- Python 3.6 or later in the
PATHof the Node-RED process. The node triespython3and thenpython(on Windowspython,python3andpy) and uses the first one that runs. If that one is older than 3.6, it reportsPython 3.6+ requiredand does not try the next. - The
venvmodule of that Python. On Debian and Ubuntu it is a separate package:sudo apt install python3-venv. - Access to the package index that pip is configured for, to install packages.
Properties
| Property | Description |
|---|---|
| Name | Name of the environment. Required. The directory of the environment is derived from it |
Creating an environment
- Open a python executor node, and in Environment choose to add a new
python-environment. - The dialog looks for Python and shows the version and the path it found, or what is missing.
- Write a Name. If an environment with that name exists already, the dialog shows
Environment already existsand its packages, and the node uses it as it is. - Otherwise press Create Environment. The node runs
python -m venvwith the Python it found, upgrades pip inside the new environment, and showsEnvironment createdwith the Python version. - Install the packages the code needs (see Managing packages), add the node, and deploy.
Once the environment exists, the Name field is disabled.
A node saved without pressing Create Environment has no environment behind it. Node-RED logs Python environment "<name>" not found or invalid when it starts the flows, and the python-executor nodes that use it fail: see Errors.
Managing packages
The dialog of an existing environment lists its packages with their versions, as pip list gives them.
- Install: write what you would pass to
pip installand press Install or Enter. Several requirements are separated by spaces:numpy opencv-python-headless. A version goes without spaces inside the requirement:pandas>=1.0. Options of pip are accepted:--upgrade numpy. The output of pip appears in the dialog as it is produced, followed byInstallation completeorInstallation failedwith the exit code. - Commas do not separate requirements. The placeholder of the field shows a list with commas, but
numpy, pandasfails: pip receivesnumpy,and rejects it as an invalid requirement. - Uninstall: the trash button of a package runs
pip uninstallfor it, after a confirmation.pip,setuptoolsandwheelhave no button. - The button next to Installed Packages reads the list again. Right after Create Environment in a new configuration node the list stays empty until this button is pressed or a package is installed.
Packages are installed and removed at once, without a deploy. In cold mode the next message sees the change. In hot mode a worker that is running can import a newly installed package, and keeps the modules it has already imported: see Hot mode.
Deleting an environment
Delete Environment removes the directory of the environment, with all its packages, after a confirmation. It happens at once, also if the dialog is then canceled. The nodes that still select the environment no longer have an interpreter to start: see Errors.
Deleting the configuration node in the editor does not remove the directory.
Where environments are stored
~/.node-red/python-envs/<directory>/~is the home directory of the user that runs Node-RED. The path does not follow the user directory of Node-RED: with anotheruserDir, the environments are still under~/.node-red/python-envs.<directory>is the name in lower case, with every character other thana-z,0-9,_and-replaced by_, repeated_joined,_removed from both ends, and cut to 50 characters.My Env #1 (Vision)givesmy_env_1_vision.- Two names that give the same directory are the same environment.
- The interpreter the
python-executornodes run isbin/pythoninside the directory (Scripts\python.exeon Windows). - The file
.rosepetal-env-info.jsonin the directory records the name, the creation date, the Python version and the path of the Python that created the environment.
The environment is not part of the flow. A flow exported to another machine carries the name of the environment: there, open the configuration node, press Create Environment and install the packages again.
Errors
| Message | Where | Cause |
|---|---|---|
Python not found. Please install Python 3.6+ and ensure it is in your PATH. | Dialog | None of the Python commands runs in the PATH of the Node-RED process |
Python 3.6+ required. Found version ... | Dialog | The first Python command that runs is older than 3.6 |
The venv module is not available. ... | Dialog | Python is installed without venv |
Failed to create virtual environment: ... | Dialog, after Create Environment | python -m venv failed; the text of its error follows. The partial directory is removed |
Python environment "<name>" not found or invalid | Node-RED log, when the flows start | The directory of the environment does not exist or has no interpreter |
Python interpreter not found: <path> | python-executor in cold mode, for every message | Same cause |
Hot worker pool failed to start: spawn <path> ENOENT | python-executor in hot mode, when the flows start; then every message fails | Same cause |
Limits
- The environment is always created from the first of
python3andpythonfound in thePATH. To use another interpreter, create a virtual environment yourself and write the path of itspythonin the Python Path of thepython-executornode, without selecting an environment. - An environment cannot be renamed: the name is fixed once it exists.