Python 2 or 3 Script

Use the Python Script plugins to quickly manipulate data. All Python plugins only have one action: run an arbitrary function that you configure. Feed the function any JSON objects, including output objects from previous steps in the workflow.

InsightConnect supports both Python 2 and 3, so you can work with whichever version you prefer or are more comfortable with. Both plugins include the following third-party Python libraries: requests, maya, lxml, beautifulsoup, pyyaml, and records. Any imports must be within the run function.

Set Request Timeouts

Set a timeout value when making requests to external servers in case the server isn't responding in a timely manner. Without a timeout value, your code may hang for several minutes or more because requests don't time out unless you explicitly set a value. Hanging code can be an issue for frequently used plugins, and plugins used by multiple workflows. The plugin may become unresponsive to new jobs because it's stuck waiting for incomplete jobs. The following Requests documentation provides more details on how to set timeout values:

Import Third Party Libraries

Depending on your usage, you may want to import third party libraries to use in your Python script. To use these third party libraries, include individual libraries in the connection of the Python plugin.

Supported Modules

You can install any 3rd party module available publicly on PyPi. Other modules are not supported.

To configure the Python connection, select the Python plugin during a workflow building session or create the connection independently by choosing Plugins & Tools from the Settings tab on the left menu. On the Plugins & Tools page, select the Connections tab and click Add Connection in the upper-right corner.

In the Connection page:

  1. Input a unique and identifiable connection name and choose an orchestrator to run this plugin on.
  2. Navigate to the "Connection Parameters" section.
  3. In the Third-Party Modules field, add the third party libraries, enclosed in double quotes, that you wish to use. For example, your Third-Party modules field may look like ["urllib3", "chardet"]. Third Party Modules Connection
  4. Ensure the libraries are installed in the connection by adjusting the number of seconds to install the libraries in the Timeout field.
  5. Save this connection and verify the status by navigating to the Connections Page.

To use these libraries in your Python script, import them inside the run function of your Python script. Import Third Party Modules

Setup a Python Plugin

When creating a new step in the Workflow Builder:

  1. Choose Action Step.
  2. Search for “Python Script 2” or “Python Script 3,” depending on your preference.
  3. Click on the plugin of your choice, then click Continue.
  4. Click on Run function, then click Continue.
  5. In the Configure Details panel, type in your desired Action name.
  6. Check the box if you want the workflow to continue running even if this step fails.

Configure the "run" Function

First, pass in JSON objects created by previous steps to the function through the Input field, then customize how the function manages input and output directly in the run function. Learn how to format JSON input here.

To customize the run function:

  1. Import any needed libraries inside the run function. Both plugins include the following libraries: requests, maya, lxml, beautifulsoup, pyyaml, and records. Other additional Python libraries can be specified in the connection of the plugin.
  2. Create any local function variables if needed. You can set these to JSON objects or values of other available data types.
  3. Pass in input variables with built-in method params.get(). The run function uses the key names created by your JSON input to run Python script on your desired variables. To call these variables, add the key name enclosed by double quotes, like params.get(“keyname”).
  4. Create and format output variables in the return statement. Each return variable key must be enclosed in single quotes. To assign values to these variables, follow each key with a colon and the desired output value. These output values can be one of four things:
  • Local function variables by name
  • params.get() statements
  • Strings, enclosed in double quotes
  • Numbers
  1. Click anywhere outside the Function field to close the editor.

Logging messages To log messages with the Python plugin, import logging and call or other logging methods in the run function. print or equivalent statements will not successfully log messages due to how output is handled by the plugin.