github3.py: GitHub API Wrapper
github3.py is a comprehensive Python wrapper for the GitHub API (v3), designed with a logical organization of methods to interact with the API. It is currently at version 4.0.1, requires Python 3.7+, and maintains an active, though irregular, release cadence with frequent updates and bug fixes.
Warnings
- breaking In version 4.0.0, the `Comparison.commits` method was renamed to `original_commits`. The `commits` method now uses GitHub's API to get the full commit list out of the compare endpoint, enabling pagination/iterator functionality. This change affects how commit lists are retrieved from `Comparison` objects.
- breaking Starting with version 1.0.0, methods that iterate over collections (e.g., users, organizations, issues, pull requests, teams) now return distinct 'short' classes (e.g., `ShortUser`, `ShortOrganization`, `ShortIssue`, `ShortPullRequest`, `ShortTeam`) instead of the full object. This means if you expect full attributes immediately, you might need to explicitly fetch the full object or call `refresh()` on the 'short' object.
- gotcha When using username/password authentication with GitHub's Two-Factor Authentication (2FA) enabled, `github3.py` will prompt for a 2FA code on *every* API call, which is highly inefficient and disruptive.
- gotcha For older versions of `github3.py` (e.g., 0.9.x), retrieving the authenticated user's information was done via `gh.user()`. In newer versions (1.0.0+), the correct method is `gh.me()`. Mixing documentation versions can lead to `AttributeError`s.
- gotcha When listing repositories via the API, GitHub often refuses to return certain attributes (e.g., `source` and `parent`) to save bandwidth. These attributes will be `None` or missing.
- gotcha Versions of `github3.py` prior to 1.3.0 supported older Python versions (e.g., Python 2.x, 3.5, 3.6). Current versions (4.x) require Python 3.7 or newer. Attempting to install or run newer versions on unsupported Python interpreters will result in errors.
Install
-
pip install github3.py
Imports
- login
from github3 import login
- github3
import github3
Quickstart
import os
from github3 import login
# It's highly recommended to use a Personal Access Token (PAT) for authentication,
# especially when 2FA is enabled, to avoid repeated prompts.
# Store your token in an environment variable, e.g., GITHUB_TOKEN.
TOKEN = os.environ.get('GITHUB_TOKEN', 'YOUR_GITHUB_TOKEN') # Replace with actual token or ensure env var is set
if TOKEN and TOKEN != 'YOUR_GITHUB_TOKEN':
gh = login(token=TOKEN)
if gh:
print(f"Successfully authenticated as: {gh.me().login}")
# Example: Get your repositories
print("Your repositories:")
for repo in gh.iter_repos(type='owner'):
print(f"- {repo.name}")
else:
print("Authentication failed. Check your GITHUB_TOKEN.")
else:
print("GITHUB_TOKEN environment variable not set or is placeholder. Please set it for proper authentication.")
print("You can generate a PAT at: https://github.com/settings/tokens")