Python Reddit API Wrapper (PRAW)
PRAW (Python Reddit API Wrapper) is a Python package that provides a straightforward interface to Reddit's API. It abstracts away much of the complexity, adhering to Reddit's API rules internally, such as managing rate limits. The library is actively maintained, currently at version 7.8.1, with a regular release cadence that often includes new features and bug fixes.
Warnings
- breaking PRAW 7.x introduced significant changes to exception handling. The `APIException` class was renamed to `RedditAPIException` and now acts as a container for multiple `RedditErrorItem` objects, requiring iteration to access individual errors.
- deprecated Starting with PRAW 8, most functions and methods will no longer support positional arguments, requiring all arguments to be passed as keyword arguments.
- deprecated The configuration setting `refresh_token` (whether in `praw.ini`, as a keyword argument to `praw.Reddit`, or via `PRAW_REFRESH_TOKEN` environment variable) is deprecated.
- gotcha Attempting to perform write actions (e.g., posting, commenting, upvoting) with a read-only `praw.Reddit` instance will result in errors like `prawcore.exceptions.Redirect: Redirect to /r/subreddit/login/` or `invalid_grant`. This often happens when credentials for an authenticated session (username, password, refresh_token) are missing or incorrect.
- gotcha All requests to Reddit's API must include a unique and descriptive User-Agent string. Failure to provide one, or providing a generic one, can lead to your requests being blocked or rate-limited.
Install
-
pip install praw
Imports
- Reddit
import praw reddit = praw.Reddit(...)
Quickstart
import praw
import os
# It's recommended to use a praw.ini file or environment variables for credentials.
# For quickstart, we'll use environment variables.
reddit = praw.Reddit(
client_id=os.environ.get("REDDIT_CLIENT_ID", ""),
client_secret=os.environ.get("REDDIT_CLIENT_SECRET", ""),
password=os.environ.get("REDDIT_PASSWORD", ""),
user_agent=os.environ.get("REDDIT_USER_AGENT", "my_unique_user_agent_string_v1.0 (by u/YOUR_REDDIT_USERNAME)"),
username=os.environ.get("REDDIT_USERNAME", ""),
)
# Example: Get top 5 hot posts from r/python
print("Top 5 hot posts from r/python:")
for submission in reddit.subreddit("python").hot(limit=5):
print(f"- {submission.title} (Score: {submission.score})")
# Example: Access your inbox (requires authenticated instance)
# try:
# for message in reddit.inbox.unread(limit=5):
# print(f"New message: {message.subject}")
# message.mark_read()
# except prawcore.exceptions.ResponseException as e:
# print(f"Could not access inbox (read_only instance or invalid auth?): {e}")
# To verify if the instance is read-only:
print(f"Is Reddit instance read-only? {reddit.read_only}")