> ## Documentation Index
> Fetch the complete documentation index at: https://docs.langchain.com/llms.txt
> Use this file to discover all available pages before exploring further.

# Python REPL integration

> Integrate with the Python REPL tool using LangChain Python.

Sometimes, for complex calculations, rather than have an LLM generate the answer directly, it can be better to have the LLM generate code to calculate the answer, and then run that code to get the answer. In order to easily do that, we provide a simple Python REPL to execute commands in.

This interface will only return things that are printed - therefore, if you want to use it to calculate an answer, make sure to have it print out the answer.

<Warning>
  **Python REPL can execute arbitrary code on the host machine (e.g., delete files, make network requests). Use with caution.**
</Warning>

<Warning>
  The `langchain-experimental` package is no longer maintained. Examples that import from `langchain_experimental` may be outdated or broken. Use with caution.
</Warning>

```python theme={"theme":{"light":"catppuccin-latte","dark":"catppuccin-mocha"}}
from langchain.tools import tool
from langchain_experimental.utilities import PythonREPL
```

```python theme={"theme":{"light":"catppuccin-latte","dark":"catppuccin-mocha"}}
python_repl = PythonREPL()
```

```python theme={"theme":{"light":"catppuccin-latte","dark":"catppuccin-mocha"}}
python_repl.run("print(1+1)")
```

```text theme={"theme":{"light":"catppuccin-latte","dark":"catppuccin-mocha"}}
Python REPL can execute arbitrary code. Use with caution.
```

```text theme={"theme":{"light":"catppuccin-latte","dark":"catppuccin-mocha"}}
'2\n'
```

```python theme={"theme":{"light":"catppuccin-latte","dark":"catppuccin-mocha"}}
# You can create the tool to pass to an agent
@tool
def python_repl_tool(code: str) -> str:
    """A Python shell.

    Use this to execute python commands.

    Input should be a valid python command.

    If you want to see the output of a value, you should print it out with `print(...)`.
    """
    return python_repl.run(code)
```

***

<div className="source-links">
  <Callout icon="terminal-2">
    [Connect these docs](/use-these-docs) to Claude, VSCode, and more via MCP for real-time answers.
  </Callout>

  <Callout icon="edit">
    [Edit this page on GitHub](https://github.com/langchain-ai/docs/edit/main/src/oss/python/integrations/tools/python.mdx) or [file an issue](https://github.com/langchain-ai/docs/issues/new/choose).
  </Callout>
</div>
