Skip to main content

Actions Status codecov

A StarCraft II API Client for Python 3

An easy-to-use library for writing AI Bots for StarCraft II in Python 3. The ultimate goal is simplicity and ease of use, while still preserving all functionality. A really simple worker rush bot should be no more than twenty lines of code, not two hundred. However, this library intends to provide both high and low level abstractions.

This library (currently) covers only the raw scripted interface. At this time I don't intend to add support for graphics-based interfaces.

The documentation can be found here. For bot authors, looking directly at the files in the sc2 folder can also be of benefit: bot_ai.py, unit.py, units.py, client.py, game_info.py and game_state.py. Most functions in those files have docstrings, example usages and type hinting.

I am planning to change this fork more radically than the main repository, for bot performance benefits and to add functions to help new bot authors. This may break older bots in the future, however I try to add deprecation warnings to give a heads up notification. This means that the video tutorial made by sentdex is outdated and does no longer directly work with this fork.

For a list of ongoing changes and differences to the main repository of Dentosal, check here.

Installation

By installing this library you agree to be bound by the terms of the AI and Machine Learning License.

For this fork, you'll need Python 3.9 or newer.

Install the pypi package:

pip install --upgrade burnysc2

or directly from develop branch:

pip install --upgrade --force-reinstall https://github.com/BurnySc2/python-sc2/archive/develop.zip

Both commands will use the sc2 library folder, so you will not be able to have Dentosal's and this fork installed at the same time, unless you use virtual environments.

StarCraft II

You'll need a StarCraft II executable. If you are running Windows or macOS, just install SC2 from blizzard app.

Linux installation

You can install StarCraft II on Linux with Wine, Lutris or even the Linux binary, but the latter is headless so you cannot actually see the game. Starcraft II can be directly installed from Battlenet once it is downloaded with Lutris. By default, it will be installed here:

/home/burny/Games/battlenet/drive_c/Program Files (x86)/StarCraft II/

Next, set the following environment variables (either globally or within your development environment, e.g. Pycharm: Run -> Edit Configurations -> Environment Variables):

SC2PF=WineLinux
WINE=/usr/bin/wine
# Or a wine binary from lutris:
# WINE=/home/burny/.local/share/lutris/runners/wine/lutris-4.20-x86_64/bin/wine64
# Default Lutris StarCraftII Installation path:
SC2PATH='/home/burny/Games/battlenet/drive_c/Program Files (x86)/StarCraft II/'

WSL

When running WSL in Windows, python-sc2 detects WSL by default and starts Windows Starcraft 2 instead of Linux Starcraft 2. If you wish to instead have the game played in Linux, you can disable this behavior by setting SC2_WSL_DETECT environment variable to "0". You can do this inside python with the following code:

import os
os.environ["SC2_WSL_DETECT"] = "0"

WSL version 1 should not require any configuration. You may be asked to allow Python through your firewall.

When running WSL version 2 you need to supply the following environment variables so that your bot can connect:

SC2CLIENTHOST=<your windows IP>
SC2SERVERHOST=0.0.0.0

If you are adding these to your .bashrc, you may need to export your environment variables by adding:

export SC2CLIENTHOST
export SC2SERVERHOST

You can find your Windows IP using ipconfig /all from PowerShell.exe or CMD.exe.

Maps

You will need maps to run the library.

Official maps

Official Blizzard map downloads are available from Blizzard/s2client-proto.
Extract these maps into their respective subdirectories in the SC2 maps directory.
e.g. install-dir/Maps/Ladder2017Season1/

Bot ladder maps

Maps that are run on the SC2 AI Arena Ladder can be downloaded from the SC2 AI Arena Wiki.
Extract these maps into the root of the SC2 maps directory (otherwise ladder replays won't work).
e.g. install-dir/Maps/AcropolisLE.SC2Map

Running

After installing the library, a StarCraft II executable, and some maps, you're ready to get started. Simply run a bot file to fire up an instance of StarCraft II with the bot running. For example:

python examples/protoss/cannon_rush.py

Example

As promised, worker rush in less than twenty lines:

from sc2 import maps
from sc2.player import Bot, Computer
from sc2.main import run_game
from sc2.data import Race, Difficulty
from sc2.bot_ai import BotAI

class WorkerRushBot(BotAI):
    async def on_step(self, iteration: int):
        if iteration == 0:
            for worker in self.workers:
                worker.attack(self.enemy_start_locations[0])

run_game(maps.get("Abyssal Reef LE"), [
    Bot(Race.Zerg, WorkerRushBot()),
    Computer(Race.Protoss, Difficulty.Medium)
], realtime=True)

This is probably the simplest bot that has any realistic chances of winning the game. I have ran it against the medium AI a few times, and once in a while, it wins.

You can find more examples in the examples/ folder.

API Configuration Options

The API supports a number of options for configuring how it operates.

unit_command_uses_self_do

Set this to 'True' if your bot is issueing commands using self.do(Unit(Ability, Target)) instead of Unit(Ability, Target).

class MyBot(BotAI):
    def __init__(self):
        self.unit_command_uses_self_do = True

raw_affects_selection

Setting this to true improves bot performance by a little bit.

class MyBot(BotAI):
    def __init__(self):
        self.raw_affects_selection = True

distance_calculation_method

The distance calculation method:

  • 0 for raw python
  • 1 for scipy pdist
  • 2 for scipy cdist
class MyBot(BotAI):
    def __init__(self):
        self.distance_calculation_method: int = 2

game_step

On game start or in any frame actually, you can set the game step. This controls how often your bot's step method is called.
Do not set this in the __init__ function as the client will not have been initialized yet!

class MyBot(BotAI):
    def __init__(self):
        pass  # don't set it here!

    async def on_start(self):
        self.client.game_step: int = 2

Community - Help and support

You have questions but don't want to create an issue? Join the SC2 AI Arena Discord server. Questions about this repository can be asked in text channel #python. There are discussions and questions about SC2 bot programming and this repository every day.

Bug reports, feature requests and ideas

If you have any issues, ideas or feedback, please create a new issue. Pull requests are also welcome!

Contributing & style guidelines

Git commit messages use imperative-style messages, start with capital letter and do not have trailing commas.

To run pre-commit hooks (which run autoformatting and autosort imports) you can run

uv run pre-commit install
uv run pre-commit run --all-files --hook-stage pre-push

Metadata

Release files for burnysc2 7.3.0

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for burnysc2 7.3.0
File Size Uploaded
burnysc2-7.3.0.tar.gz 168.1 kB Details

Built distribution (wheel)

Table of built distributions (wheels) for burnysc2 7.3.0
File Interpreter ABI Platform
burnysc2-7.3.0-py3-none-any.whl Python 3 none any Details

Total release size: 352.1 kB

Release files / burnysc2-7.3.0.tar.gz

Download URL burnysc2-7.3.0.tar.gz
Size 168.1 kB
Tags Source
SHA-256 checksum
How to use checksums
5178787dfe69095c11a1f554cb15b87f3ead767e57c09492215f927c4131caaf
BLAKE2b-256 checksum
How to use checksums
c544ace551f024f9d4f453b225a8ad6c88963ce393db60fc0cd19cf9ab7cebee
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via uv/0.11.7 {"installer":{"name":"uv","version":"0.11.7","subcommand":["publish"]},"python":null,"implementation":{"name":null,"version":null},"distro":{"name":"Ubuntu","version":"24.04","id":"noble","libc":null},"system":{"name":null,"release":null},"cpu":null,"openssl_version":null,"setuptools_version":null,"rustc_version":null,"ci":true}

Release files / burnysc2-7.3.0-py3-none-any.whl

Download URL burnysc2-7.3.0-py3-none-any.whl
Size 184.0 kB
Tags Python 3
SHA-256 checksum
How to use checksums
6250a556ffa4d7a052d99676d8c974f84115828fa50e50ca64168589b91598c7
BLAKE2b-256 checksum
How to use checksums
995d9cf4f8df16e9c44cf1d6b42907bccadfc471612c46c3e04b0b60fb1e15e1
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via uv/0.11.7 {"installer":{"name":"uv","version":"0.11.7","subcommand":["publish"]},"python":null,"implementation":{"name":null,"version":null},"distro":{"name":"Ubuntu","version":"24.04","id":"noble","libc":null},"system":{"name":null,"release":null},"cpu":null,"openssl_version":null,"setuptools_version":null,"rustc_version":null,"ci":true}

Release history Release notifications | RSS feed

This release

7.3.0 This release

2 release files

7.2.1

2 release files

7.2.0

2 release files

7.1.3

2 release files

7.1.2

2 release files

7.1.1

2 release files

7.1.0

2 release files

7.0.7

2 release files

7.0.6

2 release files

7.0.5

2 release files

7.0.3

2 release files

7.0.1

2 release files

7.0.0

2 release files

6.6.0

2 release files

6.5.0

2 release files

6.4.0

2 release files

6.3.0

2 release files

6.2.0

2 release files

6.1.1

2 release files

6.1.0

2 release files

6.0.7

2 release files

6.0.6

2 release files

6.0.5

2 release files

6.0.4

2 release files

6.0.3

2 release files

6.0.2

2 release files

6.0.1

2 release files

6.0.0

2 release files

5.0.15

2 release files

5.0.14

2 release files

5.0.13

2 release files

5.0.11

2 release files

5.0.10

2 release files

5.0.9

2 release files

5.0.8

2 release files

5.0.7

2 release files

5.0.6

2 release files

5.0.5

2 release files

5.0.4

2 release files

5.0.3

2 release files

5.0.2

2 release files

5.0.1

2 release files

5.0.0

2 release files

4.11.9

2 release files

4.11.8

2 release files

4.11.7

2 release files

4.11.6

2 release files

4.11.5

2 release files

4.11.2

2 release files

4.11.1

2 release files

4.11.0

2 release files

0.12.9

2 release files

0.12.6

2 release files

0.12.5

1 release file

0.12.4

2 release files

0.12.3

2 release files

0.12.2

1 release file

0.12.1

1 release file

0.12.0

2 release files

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page