--- title: Python Interpreter slug: /python-interpreter --- import Icon from "@site/src/components/icon"; import Tabs from '@theme/Tabs'; import TabItem from '@theme/TabItem'; 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](/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](/calculator) 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](/img/component-python-interpreter.png) 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: ```text Executed evaluate_expression Input: { "expression": "2+5" } Output: { "result": "7" } ``` 5. 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. ```python import pandas as pd import math # Create a simple DataFrame df = pd.DataFrame({ 'numbers': [1, 2, 3, 4, 5], 'squares': [x**2 for x in range(1, 6)] }) # Calculate the square root of the mean result = math.sqrt(df['squares'].mean()) print(f"Square root of mean squares: {result}") ``` The agent correctly chooses the `run_python_repl` tool to solve the problem. Result: ```text Executed run_python_repl Input: { "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}\")" } Output: { "result": "Square root of mean squares: 3.3166247903554" } ``` 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. ## Pass inputs to the Python Interpreter To pass inputs to the **Python Interpreter** component, you need to customize the component's code to add input fields. After the input field is added to the component code, the port becomes available for connections. For example, to connect a [**Text** component](/legacy-core-components) and pass a URL value to the **Python Interpreter** component, do the following: 1. Add a **Python Interpreter** component to your flow. 2. To modify the **Python Interpreter** component's code, click