publictransport
A Python client for public transport APIs. One model for stops, departures and trips covers servers that speak EFA, MOTIS, TRIAS 1.2, OJP 1.0, HAFAS mgate or OpenTripPlanner 2, so an application queries every supported network the same way.
uv add publictransport # or: pip install publictransport
There are wheels for Linux (glibc), macOS and Windows on x86-64, and for the first two on ARM. The package wraps the Rust library of the same name, which builds the requests and parses the answers, and httpx sends them.
from publictransport import BundledNetwork, Client, Coordinate, LocationKind
network = BundledNetwork.VVO_EFA.network()
with Client(user_agent="my-app/1.0 (+https://example.org)") as client:
stops = client.locations(network, "Hauptbahnhof", kinds=[LocationKind.STOP])
for departure in client.departures(network, stops[0], limit=10):
print(departure.scheduled_time, departure.line.name, departure.direction)
nearby is the second way to find stops: it lists the stops around a position, the nearest first. Coordinate.distance_meters gives the straight-line distance between two positions in meters.
position = Coordinate(latitude=51.04, longitude=13.7325)
for stop in client.nearby(network, position, radius_meters=500):
if stop.coordinate:
print(stop.name, round(position.distance_meters(stop.coordinate)), "m")
arrivals takes the same arguments and lists what arrives at the stop. An arrival has an origin, where the vehicle comes from, in place of the direction.
arrivals = client.arrivals(network, stops[0], limit=10)
journey gives the run of one vehicle: every stop it calls at, with times and platforms. It takes the journey_id of a departure, an arrival or a leg of a trip. The ID only works on the network it came from, and for about a day. A stopover gives each of its sides with departure() and arrival() as an end of a leg, which has the delay and the local time, and platform() works on it as on a departure.
departure = client.departures(network, stops[0], limit=1)[0]
if departure.journey_id:
journey = client.journey(network, departure.journey_id)
for stopover in journey.stops:
# The last stop has no departure.
end = stopover.departure() or stopover.arrival()
print(end.local_expected_time() if end else None, stopover.stop.name)
AsyncClient has the same methods as coroutines. The documentation has a program for each kind of request, which are also in examples.
Trips
found = client.trips(network, stops[0], stops[1], limit=5)
for trip in found.trips:
print(trip.duration_seconds(), "seconds,", trip.transfers(), "changes")
# The same arguments again, with the reference to the trips after these.
more = client.trips(network, stops[0], stops[1], limit=5, page=found.later)
An end of a trip is a Location, the ID of a stop as text or a Coordinate. depart_at= and arrive_by= ask for another time than now, as a timezone-aware datetime.
home = Coordinate(latitude=51.04, longitude=13.73)
client.trips(network, home, "de:14612:28", arrive_by=tomorrow_at_nine)
Networks
from publictransport import Coordinate, Registry
registry = Registry.bundled()
network = registry.get("vrr-efa") # None if no network has this ID
# The networks for a position, the best coverage first.
for covering in registry.covering(Coordinate(latitude=51.22, longitude=6.79)):
print(covering.tier, covering.network.name)
The list of bundled networks also says what a network of your own needs. Write one as Network(id=..., name=..., endpoint=..., protocol=Protocol.EFA, timezone="Europe/Berlin"), or read one from an entry of transport-apis with network_from_json(id, json). A header for a key or a client name is one more entry in its list:
network.headers.append(Header(name="ET-Client-Name", value="mycompany-myapp"))
Times, delays and platforms
Times are timezone-aware datetime objects in UTC. A departure, an arrival and an end of a leg also give them as the clocks at their stop show them, which is what to print.
departure.local_scheduled_time() # None where the time zone of the stop is unknown
departure.local_expected_time() # the real-time one, if there is one
departure.delay_seconds() # None without real-time data
departure.platform() # the real-time platform, otherwise the scheduled one
stop.local_time(any_time) # the same for any time at a location
Disruptions and hints
A departure, a leg of a trip and the run of a vehicle have remarks: the texts that the server sends with them. Every protocol but MOTIS fills them. A remark is one of two kinds. A disruption is a message from the incident system of the operator, such as a closed road or a broken elevator. A hint is a fixed property of the ride, such as air conditioning or a note for wheelchair users.
for remark in departure.remarks:
if remark.kind == RemarkKind.DISRUPTION:
print(remark.title or remark.text)
print(remark.url) # a page with more about it, or None
print(remark.valid_until) # None where the server does not say
At least one of title and text is set. text is plain text without HTML, in the language the server answers in. The language of the network asks for one, and the list of networks says what servers make of it. The server sends a disruption with every departure and leg it concerns, so an application that shows each message once tells them apart by id. A hint has no id. It has the code that the server gives its kind, such as A__KL for air conditioning on German EFA servers, for an application that shows icons for the ones it knows. Operators define these codes, so they differ between networks.
Errors
from publictransport import Error
try:
client.departures(network, "no-such-stop")
except Error.NotFound:
...
except Error.RateLimited as error:
print(error.retry_after_seconds) # how long the server asks to wait, or None
What the server or the query cause is a subclass of publictransport.Error. A failed HTTP exchange is raised as httpx raises it, and an answer above 16 MB raises ResponseTooLarge.
Your own HTTP client
Client(http_client=...) builds on an httpx client of yours, for example to use a proxy. That client has to set the User-Agent itself and must not follow redirects.
An application with another HTTP stack skips the clients. The network builds each request and reads its answer, and the same pair exists for nearby stops, departures, arrivals and trips:
request = network.locations_request(query) # a LocationQuery
locations = network.parse_locations(response)
Known limitations
The model classes are generated by UniFFI, and type checkers stumble over the generated code in two places. Both work at runtime; a typed application needs an ignore comment there.
Place.ID("..."),Place.COORDINATE(...)and theTripTimevariants are rejected where aPlaceorTripTimeis expected. Each takes its one value as a positional argument.- pyright rejects
except Error:. Catching a subclass such asError.NotFoundis accepted, and the fields of the subclasses are untyped.
str() of an error shows only its fields and is empty for errors without any, such as Error.Blocked. Use repr() in messages.
Development
The package is built with maturin from the crate in bindings/uniffi. Inside the repository's nix shell:
uv sync # build the Rust library, install into .venv
uv sync --reinstall-package publictransport # again after a change to the Rust code
ruff check . && ruff format --check . && pyright
uv run pytest
Metadata
Release files for publictransport 0.2.0
For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.
Source distribution (sdist)
| File | Size | Uploaded | Provenance | Actions |
|---|---|---|---|---|
| publictransport-0.2.0.tar.gz | 480.5 kB | With provenance | Details for publictransport-0.2.0.tar.gz |
Built distributions (wheels)
| File | Actions Reset | |||
|---|---|---|---|---|
| publictransport-0.2.0-py3-none-win_amd64.whl | Python 3 | none | Windows x86-64 | Details for publictransport-0.2.0-py3-none-win_amd64.whl |
| publictransport-0.2.0-py3-none-manylinux2014_x86_64.manylinux_2_17_x86_64.whl | Python 3 | none | Linux glibc 2.17+ x86-64 | Details for publictransport-0.2.0-py3-none-manylinux2014_x86_64.manylinux_2_17_x86_64.whl |
| publictransport-0.2.0-py3-none-manylinux2014_aarch64.manylinux_2_17_aarch64.whl | Python 3 | none | Linux glibc 2.17+ ARM64 | Details for publictransport-0.2.0-py3-none-manylinux2014_aarch64.manylinux_2_17_aarch64.whl |
| publictransport-0.2.0-py3-none-macosx_11_0_arm64.whl | Python 3 | none | macOS 11.0+ ARM64 | Details for publictransport-0.2.0-py3-none-macosx_11_0_arm64.whl |
| publictransport-0.2.0-py3-none-macosx_10_12_x86_64.whl | Python 3 | none | macOS 10.12+ x86-64 | Details for publictransport-0.2.0-py3-none-macosx_10_12_x86_64.whl |
Total release size: 8.8 MB
Release files / publictransport-0.2.0.tar.gz
| Download URL | publictransport-0.2.0.tar.gz |
|---|---|
| Size | 480.5 kB |
| Tags | Source |
|
SHA-256 checksum How to use checksums |
2554a9db148a135ff5246621ada7005e87cb67aaeb8e89c31626925f554b9f95
|
|
BLAKE2b-256 checksum How to use checksums |
d82bd72cc7cdff0221dcee48453818aa1b32be25f58815d96e6f47486f9af353
|
| 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 Nov 24, 2026.
Transparency logRelease files / publictransport-0.2.0-py3-none-win_amd64.whl
| Download URL | publictransport-0.2.0-py3-none-win_amd64.whl |
|---|---|
| Size | 1.5 MB |
| Tags | Python 3 Windows x86-64 |
|
SHA-256 checksum How to use checksums |
94b394998ba525ec6094093700e9620e5ad33bd4f7e3358329bb62e796205bd1
|
|
BLAKE2b-256 checksum How to use checksums |
6f30a6073eae209e432c48d6ee76a093a617c45d69fc9cebc87633918e57f2f6
|
| 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 Nov 24, 2026.
Transparency logRelease files / publictransport-0.2.0-py3-none-manylinux2014_x86_64.manylinux_2_17_x86_64.whl
| Download URL | publictransport-0.2.0-py3-none-manylinux2014_x86_64.manylinux_2_17_x86_64.whl |
|---|---|
| Size | 1.8 MB |
| Tags | Linux glibc 2.17+ x86-64 Python 3 |
|
SHA-256 checksum How to use checksums |
0eb7a05694f1333de1e7546c89c192297004c9b59dc99a1f48b450ec95d7c066
|
|
BLAKE2b-256 checksum How to use checksums |
fcc1ca6b7948965994f10d3ea3166df54958f54314a0f81a302ddc863d97c3bf
|
| 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 Nov 24, 2026.
Transparency logRelease files / publictransport-0.2.0-py3-none-manylinux2014_aarch64.manylinux_2_17_aarch64.whl
| Download URL | publictransport-0.2.0-py3-none-manylinux2014_aarch64.manylinux_2_17_aarch64.whl |
|---|---|
| Size | 1.8 MB |
| Tags | Linux glibc 2.17+ ARM64 Python 3 |
|
SHA-256 checksum How to use checksums |
226df91b59a2aef03ee5d6af4ea4fba96ad86a84bc9f881800dc839e152eec43
|
|
BLAKE2b-256 checksum How to use checksums |
692671228c50b3c27fcd825af46c440670122e2bd98191e0f3e6bfaae2d6374f
|
| 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 Nov 24, 2026.
Transparency logRelease files / publictransport-0.2.0-py3-none-macosx_11_0_arm64.whl
| Download URL | publictransport-0.2.0-py3-none-macosx_11_0_arm64.whl |
|---|---|
| Size | 1.6 MB |
| Tags | Python 3 macOS 11.0+ ARM64 |
|
SHA-256 checksum How to use checksums |
09daecb8b351cf269af7871d4b5d09da6cc89883fe0f0e9386b373112ca789d8
|
|
BLAKE2b-256 checksum How to use checksums |
7f39a92e22fd899882e1f19f9d0f6934f121c4218707a28ebdec5407fcd7decf
|
| 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 Nov 24, 2026.
Transparency logRelease files / publictransport-0.2.0-py3-none-macosx_10_12_x86_64.whl
| Download URL | publictransport-0.2.0-py3-none-macosx_10_12_x86_64.whl |
|---|---|
| Size | 1.6 MB |
| Tags | Python 3 macOS 10.12+ x86-64 |
|
SHA-256 checksum How to use checksums |
5654fb85aa1ed11c6834582cc22bc4c3c92c2996c6e07d6d4d01b4ce2fc0828c
|
|
BLAKE2b-256 checksum How to use checksums |
41940a97ba2465d08bd4c1bb86d3c9847734105db706d2e47a3865d383ff069a
|
| 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 Nov 24, 2026.
Transparency logSecurity for publictransport 0.2.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
All 6 files in this release have been uploaded with provenance metadata.
Attestation for publictransport-0.2.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 Nov 24, 2026.
Transparency logA 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.