Skip to main content

Python Interpreter

This component allows you to execute Python code with imported packages.

The Python Interpreter component can only import packages that are already installed in your Langflow environment. If you encounter an ImportError when trying to use a package, you need to install it first.

To install custom packages, see Install custom dependencies.

Use the Python Interpreter in a flow

  1. To use this component in a flow, in the Global Imports field, add the packages you want to import as a comma-separated list, such as math,pandas. At least one import is required.
  2. In the Python Code field, enter the Python code you want to execute. Use print() to see the output.
  3. Optional: Enable Tool Mode, and then connect the Python Interpreter component to an Agent component as a tool. For example, connect a Python Interpreter component and a Calculator component as tools for an Agent component, and then test how it chooses different tools to solve math problems. Python Interpreter and Calculator components connected to an Agent component
  4. Ask the agent an easier math question. The Calculator tool can add, subtract, multiple, divide, or perform exponentiation. The agent executes the evaluate_expression tool to correctly answer the question.

Result:


_10
Executed evaluate_expression
_10
Input:
_10
{
_10
"expression": "2+5"
_10
}
_10
Output:
_10
{
_10
"result": "7"
_10
}

  1. Give the agent complete Python code. This example creates a Pandas DataFrame table with the imported pandas packages, and returns the square root of the mean squares.

_12
import pandas as pd
_12
import math
_12
_12
# Create a simple DataFrame
_12
df = pd.DataFrame({
_12
'numbers': [1, 2, 3, 4, 5],
_12
'squares': [x**2 for x in range(1, 6)]
_12
})
_12
_12
# Calculate the square root of the mean
_12
result = math.sqrt(df['squares'].mean())
_12
print(f"Square root of mean squares: {result}")

The agent correctly chooses the run_python_repl tool to solve the problem.

Result:


_12
Executed run_python_repl
_12
_12
Input:
_12
_12
{
_12
"python_code": "import pandas as pd\nimport math\n\n# Create a simple DataFrame\ndf = pd.DataFrame({\n 'numbers': [1, 2, 3, 4, 5],\n 'squares': [x**2 for x in range(1, 6)]\n})\n\n# Calculate the square root of the mean\nresult = math.sqrt(df['squares'].mean())\nprint(f\"Square root of mean squares: {result}\")"
_12
}
_12
Output:
_12
_12
{
_12
"result": "Square root of mean squares: 3.3166247903554"
_12
}

If you don't include the package imports in the chat, the agent can still create the table using pd.DataFrame, because the pandas package is imported globally by the Python Interpreter component in the Global Imports field.

Python Interpreter parameters

NameTypeDescription
global_importsStringInput parameter. A comma-separated list of modules to import globally, such as math,pandas,numpy.
python_codeCodeInput parameter. The Python code to execute. Only modules specified in Global Imports can be used.
resultsDataOutput parameter. The output of the executed Python code, including any printed results or errors.
Search