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
- 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. - In the Python Code field, enter the Python code you want to execute. Use
print()to see the output. - 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.

- Ask the agent an easier math question.
The Calculator tool can add, subtract, multiple, divide, or perform exponentiation.
The agent executes the
evaluate_expressiontool to correctly answer the question.
Result:
_10Executed evaluate_expression_10Input:_10{_10 "expression": "2+5"_10}_10Output:_10{_10 "result": "7"_10}
- Give the agent complete Python code.
This example creates a Pandas DataFrame table with the imported
pandaspackages, and returns the square root of the mean squares.
_12import pandas as pd_12import math_12_12# Create a simple DataFrame_12df = 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_12result = math.sqrt(df['squares'].mean())_12print(f"Square root of mean squares: {result}")
The agent correctly chooses the run_python_repl tool to solve the problem.
Result:
_12Executed run_python_repl_12_12Input:_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}_12Output:_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
| Name | Type | Description |
|---|---|---|
| global_imports | String | Input parameter. A comma-separated list of modules to import globally, such as math,pandas,numpy. |
| python_code | Code | Input parameter. The Python code to execute. Only modules specified in Global Imports can be used. |
| results | Data | Output parameter. The output of the executed Python code, including any printed results or errors. |