# How to Import the Computer API Directly into Python Code with Open Interpreter

> Learn to import the computer API directly into Python code with Open Interpreter. Set a simple flag to enable seamless integration for your scripts.

- Repository: [Open Interpreter/open-interpreter](https://github.com/openinterpreter/open-interpreter)
- Tags: how-to-guide
- Published: 2026-03-05

---

**Set `interpreter.computer.import_computer_api = True` to expose the pre-initialized `computer` instance as a normal Python module, allowing standard `import computer` statements in scripts executed by the interpreter.**

The Open Interpreter framework provides a powerful **Computer API** that enables language models to control the host machine through mouse movements, keyboard input, screenshots, and file system operations. While this API is automatically available as `interpreter.computer` during chat sessions, developers often need to import the computer API directly into standalone Python scripts for programmatic automation.

## Understanding the Computer API Architecture

By default, Open Interpreter instantiates a single shared `Computer` object at `interpreter.computer` when you initialize the `OpenInterpreter` class. This object manages all low-level system interactions through submodules like `computer.mouse`, `computer.keyboard`, and `computer.display`.

The source code in [`interpreter/core/computer/computer.py`](https://github.com/openinterpreter/open-interpreter/blob/main/interpreter/core/computer/computer.py) explicitly warns against manually importing the `computer` package. Doing so creates a second, unmanaged instance that bypasses the interpreter's safety controls and state management. Instead, the framework provides a controlled mechanism to expose the existing instance through Python's module system.

## Enabling Direct Import with import_computer_api

The `OpenInterpreter` class stores a boolean flag called `import_computer_api` on the `Computer` object itself. You can access this attribute at `interpreter.computer.import_computer_api`.

According to the initialization logic in [`interpreter/core/core.py`](https://github.com/openinterpreter/open-interpreter/blob/main/interpreter/core/core.py) (lines 18-22), this flag defaults to `False` for safety. When set to `True`, the interpreter injects a helper module into the execution environment before running any user-supplied Python code.

## How the Import Mechanism Works

When `import_computer_api` is enabled, the interpreter prepends a small code block defined in [`interpreter/core/computer/terminal/terminal.py`](https://github.com/openinterpreter/open-interpreter/blob/main/interpreter/core/computer/terminal/terminal.py). This helper executes the following logic:

```python
import sys
sys.modules["computer"] = interpreter.computer

```

This registration adds the pre-initialized `computer` instance to Python's `sys.modules` cache under the name `"computer"`. Subsequent `import computer` statements anywhere in the execution context will retrieve this shared instance rather than attempting to load a new package from disk.

Simultaneously, the system prompt construction in [`interpreter/core/computer/computer.py`](https://github.com/openinterpreter/open-interpreter/blob/main/interpreter/core/computer/computer.py) (lines 65-77) adjusts its instructions to the language model. When the flag is `False`, the system message warns the model not to import the computer module. When `True`, it omits this warning, allowing the model to generate code with standard import statements.

## Practical Code Examples

### Enabling the Flag Interactively

Start an interpreter session and enable direct imports before executing code:

```python
from interpreter import OpenInterpreter

interpreter = OpenInterpreter()
interpreter.computer.import_computer_api = True

# Now Python code executed by the interpreter can use 'import computer'

interpreter.run("""
import computer
computer.mouse.move(x=500, y=500)
""")

```

### Writing Scripts That Import Computer

Once the flag is enabled, write natural Python code that imports the API as a standard module:

```python
script = """
import computer

# Capture screenshot using the display submodule

screenshot = computer.display.screenshot()
print(f"Captured screenshot: {screenshot.base64_image[:50]}...")

# Type text using the keyboard submodule

computer.keyboard.type("Hello from imported computer API", interval=0.01)
"""

interpreter.run(script)

```

The `computer.display.screenshot()` method referenced here is implemented in [`interpreter/core/computer/display/display.py`](https://github.com/openinterpreter/open-interpreter/blob/main/interpreter/core/computer/display/display.py), while `computer.keyboard.type` lives in [`interpreter/core/computer/keyboard/keyboard.py`](https://github.com/openinterpreter/open-interpreter/blob/main/interpreter/core/computer/keyboard/keyboard.py).

### Profile Configuration

Built-in profiles such as "the01" automatically enable this feature. You can replicate this in a custom YAML profile:

```yaml

# my_profile.yaml

import_computer_api: true
system_message: "You are a helpful assistant with full computer access."

```

When the interpreter loads this profile, any Python code it executes can immediately `import computer` without additional setup.

### Temporary Toggle

For safety, enable the flag only for specific operations:

```python

# Enable for sensitive automation

interpreter.computer.import_computer_api = True
interpreter.run("import computer; computer.mouse.click()")

# Disable to prevent accidental imports in subsequent calls

interpreter.computer.import_computer_api = False

```

## Safety Mechanisms and Edge Cases

The Open Interpreter source code includes safeguards to prevent recursive or conflicting API usage. In [`interpreter/core/computer/ai/ai.py`](https://github.com/openinterpreter/open-interpreter/blob/main/interpreter/core/computer/ai/ai.py), the interpreter temporarily disables the `import_computer_api` flag while processing its own internal AI calls. This prevents the computer API from being imported during recursive interpreter operations, which could break the execution environment or create circular dependencies.

Always ensure that `import_computer_api` remains `False` when executing untrusted code, as enabling it exposes the full computer control API through a standard import statement, potentially allowing malicious scripts to automate mouse and keyboard actions without explicit permission.

## Key Source Files Reference

- **[`interpreter/core/core.py`](https://github.com/openinterpreter/open-interpreter/blob/main/interpreter/core/core.py)** — Stores the `import_computer_api` flag on the `Computer` object during interpreter initialization.
- **[`interpreter/core/computer/computer.py`](https://github.com/openinterpreter/open-interpreter/blob/main/interpreter/core/computer/computer.py)** — Constructs the system prompt that warns against manual imports when the flag is disabled.
- **[`interpreter/core/computer/terminal/terminal.py`](https://github.com/openinterpreter/open-interpreter/blob/main/interpreter/core/computer/terminal/terminal.py)** — Contains the `import_computer_api_code` helper that registers the shared instance in `sys.modules`.
- **[`interpreter/core/computer/ai/ai.py`](https://github.com/openinterpreter/open-interpreter/blob/main/interpreter/core/computer/ai/ai.py)** — Temporarily disables the flag during internal AI processing to prevent recursion.
- **[`interpreter/computer_use/tools/computer.py`](https://github.com/openinterpreter/open-interpreter/blob/main/interpreter/computer_use/tools/computer.py)** — Implements the low-level mouse, keyboard, and display actions accessible through the API.

## Summary

- The Computer API is pre-initialized at `interpreter.computer` and should not be manually imported to avoid creating duplicate instances.
- Set `interpreter.computer.import_computer_api = True` to expose the shared instance through standard `import computer` statements.
- The flag injects a helper module that registers `interpreter.computer` in `sys.modules["computer"]` before user code executes.
- Built-in profiles can enable this feature automatically via the `import_computer_api: true` YAML setting.
- Safety mechanisms in the AI module temporarily disable the flag during internal recursive calls to prevent execution conflicts.

## Frequently Asked Questions

### What happens if I import the computer package without enabling the flag?

If you attempt `import computer` without setting `interpreter.computer.import_computer_api = True`, Python will either fail to find the module or create a new, unmanaged instance of the Computer class. This bypasses the interpreter's safety controls and can lead to conflicting state between the interpreter's internal instance and your imported version.

### Can I use the Computer API outside of the Open Interpreter environment?

The Computer API is designed specifically for use within the Open Interpreter framework. While the underlying tools in [`interpreter/computer_use/tools/computer.py`](https://github.com/openinterpreter/open-interpreter/blob/main/interpreter/computer_use/tools/computer.py) could theoretically be imported separately, they lack the safety wrappers and state management provided by the interpreter's Computer class. For programmatic automation outside of Open Interpreter, consider using dedicated libraries like `pyautogui` or `mss` instead.

### Why does the flag disable itself during AI calls?

The interpreter temporarily sets `import_computer_api` to `False` while processing internal AI requests (as seen in [`interpreter/core/computer/ai/ai.py`](https://github.com/openinterpreter/open-interpreter/blob/main/interpreter/core/computer/ai/ai.py)) to prevent recursive import loops. If the flag remained enabled during these calls, the AI might attempt to import the computer module while the interpreter is already processing a computer-related request, potentially causing circular dependencies or execution environment corruption.

### How do I verify that the import is using the shared instance?

After enabling the flag and running `import computer`, you can verify the shared instance by checking the object identity:

```python
import computer
print(computer is interpreter.computer)  # Should output: True

```

If this returns `True`, the import correctly references the interpreter's pre-initialized Computer object. If `False`, you have accidentally created a separate instance and should check that `interpreter.computer.import_computer_api` was set to `True` before the import statement executed.