PyGithub
raw JSON → 2.6.0 verified Tue May 12 auth: no python install: verified quickstart: stale
Python library to access the GitHub REST API v3. Provides typed Python objects for repositories, issues, pull requests, users, organizations, and more. Built on top of requests. Current version is 2.6.0 (2025). Supports Python 3.8+.
pip install PyGithub Common errors
error ModuleNotFoundError: No module named 'github' ↓
cause The Python package is installed as `PyGithub` but developers often attempt to import it using `import github` or `from github import Github`, which refers to a module name that doesn't exist directly.
fix
Ensure the library is installed with
pip install PyGithub and then import the main class correctly using from github import Github. error github.GithubException.GithubException: 401 {"message": "Bad credentials"} ↓
cause This error occurs when the provided GitHub Personal Access Token (PAT) is invalid, expired, lacks the necessary permissions (scopes), or when an incorrect base URL is used for GitHub Enterprise.
fix
Verify that your PAT is correct, active, and has the required scopes. For GitHub Enterprise, ensure the
base_url parameter is set correctly when initializing the Github object, typically to https://{hostname}/api/v3. For PATs, initialize with from github import Auth; auth = Auth.Token('YOUR_PAT'); g = Github(auth=auth). error AttributeError: 'Issue' object has no attribute 'get_issue_comments' ↓
cause This `AttributeError` typically arises when attempting to call a method or access an attribute that does not exist on a specific PyGithub object. Developers might guess method names (e.g., `get_issue_comments`) instead of consulting the actual API or using introspection.
fix
Consult the PyGithub documentation for the correct method names (e.g.,
issue.get_comments() instead of issue.get_issue_comments()). You can also use dir(obj) or help(obj) in a Python console to inspect available attributes and methods for an object. error ValueError: Invalid header value b'token <properly_formed_token>\n' ↓
cause This error often happens when a Personal Access Token (PAT) is read from a file or an environment variable and includes extraneous whitespace characters, such as a newline character (`\n`), which makes the HTTP header invalid.
fix
Ensure that the token string has no leading or trailing whitespace by using the
.strip() method: token = token_file.read().strip() or token = os.environ.get('GITHUB_TOKEN').strip(). Warnings
breaking In v2.0, the login_or_token, password, and jwt parameters on Github() were removed. Authentication must use Auth objects. ↓
fix Replace Github(login_or_token='TOKEN') with Github(auth=Auth.Token('TOKEN')).
breaking The import module name is 'github', not 'pygithub'. Using 'import pygithub' raises ModuleNotFoundError. ↓
fix Use 'from github import Github' not 'import pygithub'.
gotcha PaginatedList objects are lazily loaded. Iterating over get_issues(), get_pulls(), etc. makes one API call per page (default 30 items). Large repos can exhaust rate limits quickly. ↓
fix Use totalCount before iterating, slice results, or check g.get_rate_limit() to monitor usage.
gotcha Github objects hold a requests Session. Failing to call g.close() or use a context manager leaks connections. ↓
fix Use 'with Github(auth=auth) as g:' or explicitly call g.close() when done.
gotcha GitHub API rate limit is 5000 requests/hour for authenticated users, 60/hour for unauthenticated. PyGithub does not auto-retry on 403 rate limit errors. ↓
fix Check g.get_rate_limit().core.remaining and implement retry logic or use retry_after from the RateLimitExceededException.
deprecated GithubIntegration constructor parameters changed in v2.0. The old positional (integration_id, private_key) signature is removed. ↓
fix Use GithubIntegration(auth=Auth.AppAuth(app_id, private_key)) instead of positional arguments.
breaking The token provided to Auth.Token() must be a non-empty string. An AssertionError: assert len(token) > 0 indicates an empty or invalid token was passed during authentication. ↓
fix Ensure that the authentication token passed to Auth.Token() is a valid, non-empty string.
breaking Auth.Token() raises AssertionError if the token provided is an empty string. A valid, non-empty GitHub Personal Access Token is required. ↓
fix Ensure the token passed to Auth.Token() is a non-empty string. Verify the environment variable or configuration storing the token is correctly set and not empty.
Install compatibility verified last tested: 2026-05-12
python os / libc status wheel install import disk
3.10 alpine (musl) - - 0.84s 44.7M
3.10 slim (glibc) - - 0.62s 45M
3.11 alpine (musl) - - 1.11s 48.3M
3.11 slim (glibc) - - 0.89s 49M
3.12 alpine (musl) - - 1.39s 39.8M
3.12 slim (glibc) - - 1.34s 40M
3.13 alpine (musl) - - 0.92s 39.5M
3.13 slim (glibc) - - 0.90s 40M
3.9 alpine (musl) - - 0.81s 44.8M
3.9 slim (glibc) - - 0.74s 45M
Imports
- Github wrong
import pygithubcorrectfrom github import Github - Auth wrong
Github(login_or_token='my_token')correctfrom github import Auth - GithubIntegration
from github import GithubIntegration
Quickstart stale last tested: 2026-05-12
import os
from github import Github, Auth
token = os.environ.get('GITHUB_TOKEN', '')
auth = Auth.Token(token)
g = Github(auth=auth)
repo = g.get_repo('PyGithub/PyGithub')
print(f'Repo: {repo.full_name}')
print(f'Stars: {repo.stargazers_count}')
# List open issues
for issue in repo.get_issues(state='open')[:5]:
print(f' #{issue.number}: {issue.title}')
g.close()