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 (tiles, compact; features, full: rail tracks, signals, depots, stations, crossings, road links, terrain, ...), the road and rail networks' connectivity (network_links, network_components), 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, observe_company (a read-only view of any company), and the IDs raw commands need (depot_at, depot_tile, rail_chain, vehicle_lost).

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.1.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.1.0
File Size Uploaded Provenance Actions
pyttd-2.1.0.tar.gz 34.5 kB With provenance Details for pyttd-2.1.0.tar.gz

Built distribution (wheel)

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

Total release size: 67.0 kB

Release files / pyttd-2.1.0.tar.gz

Download URL pyttd-2.1.0.tar.gz
Size 34.5 kB
Tags Source
SHA-256 checksum
How to use checksums
dd3f611f44ba9f5dd91daf07e633ea04e1ef0eaa8441321e72d63dbeb78207b3
BLAKE2b-256 checksum
How to use checksums
21f0d416d9d646b5256a3babdc3b9ee9115bc54aa78b3e4d1687f8a9e09b85fe
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.1.0-py3-none-any.whl

Download URL pyttd-2.1.0-py3-none-any.whl
Size 32.6 kB
Tags Python 3
SHA-256 checksum
How to use checksums
fff715cfb0216cf622aba8d1c94602f2de471d541090849f415a16e45fd82997
BLAKE2b-256 checksum
How to use checksums
eb33e6815a3c52fd17ad7043f3630c1d907106fbdea061f520f6a14ffc5d3400
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.1.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.1.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.1.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