Set up Python for an environment
Python nodes, such as the Python Script node, need a Python environment to run. Provide one in either of these ways:
- Create the environment for each run with the Python Environment Provider node, on managed and sovereign environments
- Use a permanent environment that you control, with the Python Environment Reader (Labs) node on a sovereign executor
If your workflow only needs common packages, use the bundled environment instead. It requires no setup. For the included packages, see Bundled packages.
Create the environment for each run
The Python Environment Provider node passes a Python environment specification to downstream Python nodes. This option works on both managed and sovereign environments.
To provide a Python environment with this node:
- Add the Python Environment Provider node to the workflow.
- In the node, define the packages your Python scripts need.
- Connect the node to the downstream Python nodes.
The executor creates the environment from scratch each time a downstream Python node runs. When the run ends, the executor discards the environment. For detailed instructions, see Set up Pixi for Python environment management.
Use a permanent environment
Use a permanent environment when you want workflows to run with a Python environment that you control. You install the environment once on your sovereign executor, and workflow builders select it with the Python Environment Reader (Labs) node.
INFO
Permanent environments work only on sovereign executors, as you need access to the executor installation folder. Environments that Knime manages don't support them.
Install the environment
To install the environment:
- Go to your executor installation folder.
- Open the
bundlingfolder, next to theconfiguration,dropins, andfeaturesfolders. This folder exists only if the KNIME Python Integration is installed. - In the
bundlingfolder, create a folder for your environment, for examplemy-python-env. Workflow builders select the environment by this folder name. - In this folder, create the Pixi environment. It must contain
knime-python-baseand the packages your workflow builders need.knime-python-baseprovidespandas,pyarrow,py4j, and the other packages that Knime Python nodes require.
The result is a complete Pixi project. The node uses only the default Pixi environment:
text
knime/
└── bundling/
└── my-python-env/
├── .pixi/
│ └── envs/
│ └── default/
├── pixi.lock
└── pixi.tomlMake the environment read-only
Knime deletes writable environments in bundling/
Every time the executor restarts, Knime deletes the folders in bundling/ that don't belong to an installed extension. To keep your environment, make it read-only and owned by root. You can't turn this cleanup off.
To make the environment read-only:
- Import
pandasonce in the environment. Python writes cache files on the first import, which isn't possible after the next step. - Change the permissions of the environment folder:
- On Linux, make
rootthe owner and remove write access for all users. - On Windows, give administrators and
SYSTEMfull control, and give users read and execute access only.
- On Linux, make
- Restart the executor.
After each restart, the log shows Failed to delete unused Conda environment directory once for your environment. This error is expected and means that the protection works.
Use the environment in a workflow
To use the environment in a workflow:
- Add the Python Environment Reader (Labs) node to the workflow.
- In the node, keep Input Mode on Bundled Environment and select
my-python-envfrom the dropdown menu. - On the Python node, for example the Python Script node, add the Python environment input port from the node's + menu.
- Connect the Python Environment Reader (Labs) node to this port.
Update the environment
To change the packages in a permanent environment, repeat the installation steps with the updated pixi.toml and pixi.lock files. Running workflows keep using the files that are already there.
Prevent workflow builders from creating their own environments
To prevent workflow builders from creating their own environments, blacklist the Python Environment Provider node:
- Under Global administration, open the Environments page.
- On the tile of the sovereign environment, click the three dots and select Edit.
- Under Advanced settings, add
org.knime.pixi.nodes.PythonEnvironmentReaderNodeFactoryto block the node. - Save the changes.
Workflow builders can still point the Python Environment Reader (Labs) node to a pixi.toml file. This doesn't let them create an environment, because the node never installs or updates anything and needs a complete Pixi project next to the file.