A person in a hard hat and safety goggles with a clipboard stands beside a wall of panels labeled OpenCode, Analyze, Refactor, AI Models, Python Projects, LSP, Git UI, and Modes (Plan/Build).

How to Use OpenCode for AI-Assisted Python Coding

OpenCode is an open-source AI coding agent that runs in your terminal and lets you analyze and refactor a Python project through conversational commands. In this guide, you’ll install it on your system, set it up with a free Google Gemini API key, and learn the basics of how to use it in your daily programming work.

Here’s what OpenCode’s main interface looks like:

OpenCode's Initial Screen
OpenCode's Initial Screen

OpenCode works as a conversational assistant you explicitly direct. Ask it to analyze functions, refactor code, or explain issues. Press Enter to send your query, and you’ll get a response with full awareness of your project context. It supports more than seventy-five AI providers, including Anthropic, OpenAI, and Google Gemini.

If you’re a Python developer who prefers working in the terminal, OpenCode offers deliberate, context-aware assistance and a customizable AGENTS.md configuration file.

Prerequisites

Before you start working with OpenCode, you’ll need to fulfill the following prerequisites regarding your current system and working environment:

  • Python 3.11 or higher for the sample project
  • A modern terminal emulator

You also need an AI provider account. In this guide, you’ll use Google AI Studio to get a free Gemini API key. The free Gemini tier lets you follow along without any additional costs. However, you can also use Anthropic, OpenAI, or GitHub Copilot if you already have subscriptions to those services.

This guide uses a sample project consisting of a dice-rolling script. You’ll find the full source code in a collapsible block at the start of Step 2. The download below includes the starting script and the final refactored version so you can compare your work when you’re done:

You’ll also need some background knowledge of Python programming and basic experience with your operating system’s terminal or command line.

Step 1: Install and Set Up OpenCode

It’s time to install OpenCode and get it talking to a model. You’ll install the tool on your system, authenticate with Gemini using a free API key, configure a default model, and verify that OpenCode responds correctly to your Python questions before you start coding with it.

Install and Launch OpenCode

The quickest way to install OpenCode is to use the official installation script, which you can do with the following command:

Language: Shell
$ curl -fsSL https://opencode.ai/install | bash

This script detects your platform, downloads the appropriate binary, installs the tool, and adds it to your PATH.

If you prefer a package manager, you can also install OpenCode with Homebrew on macOS or Linux:

Language: Shell
$ brew install anomalyco/tap/opencode

Note that the Homebrew team maintains the official formula and updates it less frequently than the installation script above.

Alternatively, you can install it as a Node.js package using npm if you already have this tool on your system:

Language: Shell
$ npm install -g opencode-ai

If you’re on Windows, the best experience comes from using WSL (Windows Subsystem for Linux). Set up WSL first by following Microsoft’s WSL installation guide, then open a WSL terminal and run the curl command above. For optimal performance, you should store your project within the WSL filesystem rather than on a Windows drive.

Locked learning resources

Join us and get access to thousands of tutorials and a community of expert Pythonistas.

Unlock This Article

Already a member? Sign-In

Locked learning resources

The full article is for members only. Join us and get access to thousands of tutorials and a community of expert Pythonistas.

Unlock This Article

Already a member? Sign-In

About Leodanis Pozo Ramos

Leodanis is a self-taught Python developer, educator, and technical writer with over 10 years of experience.

» More about Leodanis

Each tutorial at Real Python is created by a team of developers so that it meets our high quality standards. The team members who worked on this tutorial are:

What Do You Think?

What’s your #1 takeaway or favorite thing you learned? How are you going to put your newfound skills to use? Leave a comment below and let us know.

Commenting Tips: The most useful comments are those written with the goal of learning from or helping out other students. Get tips for asking good questions and get answers to common questions in our support portal.


Looking for a real-time conversation? Visit the Real Python Community Chat or join the next “Office Hours” Live Q&A Session. Happy Pythoning!

Become a Member to join the conversation.

Keep Learning

Related Topics: intermediate ai python tools