pydiscourse
raw JSON → 1.7.0 verified Fri May 01 auth: no python
A Python client library for the Discourse API. Allows interacting with a Discourse forum programmatically: create topics, posts, users, groups, etc. Latest version 1.7.0 adds Python 3.12 support. Maintenance ongoing, releases are sporadic.
pip install pydiscourse Common errors
error ImportError: cannot import name 'DiscourseError' from 'pydiscourse' ↓
cause DiscourseError is not exported from the top-level package.
fix
Use from pydiscourse.exceptions import DiscourseError
error TypeError: __init__() takes 1 positional argument but 3 were given ↓
cause Passing host, api_username, api_key as positional arguments instead of keyword arguments.
fix
Use DiscourseClient(host='...', api_username='...', api_key='...')
Warnings
gotcha The constructor expects `api_username` and `api_key` as keyword arguments; positional usage was common before v1.6.0 and may break if arguments are passed positionally. ↓
fix Always pass host, api_username, api_key as keyword arguments.
deprecated Python 2.7, 3.4, 3.5 support dropped in v1.2.0. Python 3.7 dropped in v1.5.0. ↓
fix Use Python 3.8+ (3.12 recommended).
gotcha The `requests` library is required and must be installed separately if not automatically pulled. ↓
fix pip install requests
Imports
- DiscourseClient
from pydiscourse import DiscourseClient - DiscourseError wrong
from pydiscourse import DiscourseErrorcorrectfrom pydiscourse.exceptions import DiscourseError
Quickstart
import os
from pydiscourse import DiscourseClient
client = DiscourseClient(
host=os.environ.get('DISCOURSE_HOST', 'https://forum.example.com'),
api_username=os.environ.get('DISCOURSE_API_USER', 'system'),
api_key=os.environ.get('DISCOURSE_API_KEY', '')
)
# Get latest topics
latest = client.latest_topics()
print(latest[:2])