Skip to main content

pyttd

Finally, a way to play OpenTTD from Python. A real OpenTTD engine runs inside your Python process, and your code controls companies through OpenTTD own AI Script API - the same functions, rules, costs and command rate as any Squirrel AI. Test host-paced local games, or join multiplayer servers as an ordinary player.

pip install pyttd

Linux (x86_64, aarch64), macOS (Apple silicon, Intel) and Windows (x64); Python 3.12+.

import pyttd

game = pyttd.Game(seed=42)                 # a local game, advancing only when you step it
agent = game.create_agent()                # a new company, controlled from Python
api = agent.api                            # the AI Script API, acting for this company

town = max(api.AITownList(), key=api.AITown.GetPopulation)
print(api.AITown.GetName(town), api.AITown.GetPopulation(town))

api.AIRoad.SetCurrentRoadType(api.AIRoad.ROADTYPE_ROAD)
with api.AITestMode(), api.AIAccounting() as cost:
    api.AIRoad.BuildRoad(a, b)             # estimate only: cost.GetCosts()
api.AIRoad.BuildRoad(a, b)                 # build; waits for the command slot like an AI
game.step(30 * pyttd.TICKS_PER_DAY)        # let a month pass

How it works

One command, from Python through OpenTTD to a multiplayer server and back

The engine is OpenTTD compiled as a Python extension module. Calls go through its own Script API in the agent's script context, so a command behaves exactly as it would for a Squirrel AI. For multiplayer servers, if a command is issued too early, it waits (or raises BlockingIOError with create_agent(auto_wait=False)).

Multiplayer

game = pyttd.Game.join("example.org:3979", name="bot")   # create a company
me = game.create_agent()

The client is an ordinary player - no admin port access, or server-side AI slot are needed, and the game runs at the server's pace (step() waits). pyttd.server.query(address) reads a server's name, version, map and player counts without joining.

OpenTTD versions

A server only accepts clients of its exact OpenTTD version, and pyttd handles that for you. pip install pyttd brings the engine of the newest OpenTTD release. Game.join asks the server for its version first and, if it differs, downloads the matching engine once into pyttd's data directory. To run a given version locally, pass Game(version="15.3"), call pyttd.engines.use("15.3"), or set $PYTTD_ENGINE. One process runs one engine - you can start several processes for several games or versions.

More

  • pyttd.observe: the map as numpy arrays, and tables of towns, industries, vehicles, stations and finances.
  • pyttd.roads, pyttd.rails: A* pathfinders with bridges and tunnels, and builders for their paths.
  • pyttd.record: every command scripts execute, with decoded arguments and results; Agent.do_command issues them again.
  • pyttd.content: download Squirrel AIs from BaNaNaS and start them as opponents.
  • Game: new_game, load, save, console, and observe_company (a read-only view of any company).

See examples/.

Building

python engine/tools/build.py [--version 15.3] [--server]   # wheel -> engine/dist/

Supported OpenTTD versions are listed in engine/versions.toml.

Licence

pyttd is MIT licensed. The engine, pyttd-engine, contains OpenTTD and OpenGFX and follows them with GPL-2.0 licensing.

Metadata

Release files for pyttd 2.0.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 pyttd 2.0.0
File Size Uploaded Provenance Actions
pyttd-2.0.0.tar.gz 33.3 kB With provenance Details for pyttd-2.0.0.tar.gz

Built distribution (wheel)

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

Total release size: 64.6 kB

Release files / pyttd-2.0.0.tar.gz

Download URL pyttd-2.0.0.tar.gz
Size 33.3 kB
Tags Source
SHA-256 checksum
How to use checksums
04fd52630e05a06d30fe3cbb32d5e4b86d0a2de324498ee114617a19c08b7b6e
BLAKE2b-256 checksum
How to use checksums
304f952de8022e030dc09bbd30e66ec7f1a91a0cb98424c58afc8d9e78329721
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
Yes
Uploaded via twine/7.0.0 CPython/3.13.16

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

PyPI Publish Attestation

PyPI verified that this artifact, at this checksum, originated from the publisher listed below.

Signed by GitHub Actions, verified by PyPI on Dec 1, 2026.

Transparency log

Release files / pyttd-2.0.0-py3-none-any.whl

Download URL pyttd-2.0.0-py3-none-any.whl
Size 31.3 kB
Tags Python 3
SHA-256 checksum
How to use checksums
d58d4d8e6af9b3b151dc467bd71f94aa6e0fd3ad90c06ecfba9f1acd19243456
BLAKE2b-256 checksum
How to use checksums
ac98c239688de67944eaf78c896a935ed6a9da920579b75eca458aac2e09227b
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
Yes
Uploaded via twine/7.0.0 CPython/3.13.16

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

PyPI Publish Attestation

PyPI verified that this artifact, at this checksum, originated from the publisher listed below.

Signed by GitHub Actions, verified by PyPI on Dec 1, 2026.

Transparency log

Security for pyttd 2.0.0

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

Full provenance

Both files in this release have been uploaded with provenance metadata.

Select from 2 files

Attestation for pyttd-2.0.0.tar.gz

PyPI Publish Attestation

PyPI verified that this artifact, at this checksum, originated from the publisher listed below.

Signed by GitHub Actions, verified by PyPI on Dec 1, 2026.

Transparency log

A note on package security

PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.

To learn more about protecting yourself from malicious actors, visit our package security documentation.

Release history Release notifications | RSS feed

This release

2.0.0 This release

2 release files Full provenance

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