githubpy: Simple GitHub REST API Client
githubpy is a straightforward Python3 client designed to interact with the GitHub REST API. Its current version is 2.0.0, released in May 2023. Despite the recent PyPI release, the underlying GitHub repository shows no active development since 2017, indicating it is likely unmaintained.
Common errors
-
ImportError: cannot import name 'Github' from 'github'
cause Attempting to import `Github` from the `github` package, which belongs to the `PyGithub` library, while having `githubpy` installed.fixIf you intend to use `githubpy`, change your import statement to `from githubpy.github import Github`. If you meant to use the `PyGithub` library, install it with `pip install PyGithub` and use `from github import Github`. -
requests.exceptions.HTTPError: 403 Client Error: Forbidden for url: https://api.github.com/user/repos
cause The GitHub API returned a 403 Forbidden status, usually due to incorrect, missing, or insufficient permissions on the provided Personal Access Token, or hitting unauthenticated rate limits.fixEnsure your GitHub Personal Access Token is valid and has the necessary scopes (e.g., `repo` for repository operations, `read:user` for user profiles). Pass the token correctly during client initialization: `g = Github('YOUR_TOKEN')`. Avoid hardcoding tokens; use environment variables instead. -
AttributeError: 'dict' object has no attribute 'json'
cause Attempting to call `.json()` on an object that is already a dictionary, or trying to access attributes on a raw response before parsing it.fixThe `githubpy` client's `.get()` method returns a `requests.Response` object. You need to call `.json()` on this response to get the Python dictionary: `data = response.json()`. Access attributes on the dictionary directly, e.g., `data['login']`.
Warnings
- gotcha This library (`githubpy`) is frequently confused with the more popular and actively maintained `PyGithub` library. They have different import paths and API structures. Ensure you are using the correct library for your project.
- breaking Despite a PyPI release (v2.0.0) in May 2023, the official GitHub repository for `githubpy` (michaelliao/githubpy) has not been updated since August 2017. This suggests the project is effectively abandoned, and the 2.0.0 release might be a re-packaging without active code development or bug fixes.
- gotcha Unauthenticated requests to the GitHub API are heavily rate-limited (e.g., 60 requests per hour). Most practical uses require authentication via a Personal Access Token or GitHub App.
Install
-
pip install githubpy
Imports
- Github
from github import Github
from githubpy.github import Github
Quickstart
import os
from githubpy.github import Github
# It's recommended to use an environment variable for your token
github_token = os.environ.get('GITHUB_TOKEN', 'YOUR_GITHUB_TOKEN')
if not github_token or github_token == 'YOUR_GITHUB_TOKEN':
print("WARNING: GITHUB_TOKEN environment variable not set or placeholder used. This may lead to rate limiting or authentication errors.")
try:
g = Github(github_token)
# Get authenticated user information
user_response = g.get('/user')
user_response.raise_for_status() # Raise an exception for bad status codes (4xx or 5xx)
user_data = user_response.json()
print(f"Authenticated as: {user_data.get('login')}")
# List repositories for the authenticated user
repos_response = g.get('/user/repos')
repos_response.raise_for_status()
repos_data = repos_response.json()
print("\nUser Repositories:")
for repo in repos_data[:3]: # Print first 3 repos for brevity
print(f"- {repo.get('name')}")
except Exception as e:
print(f"An error occurred: {e}")
if hasattr(e, 'response') and e.response is not None:
print(f"Response Status: {e.response.status_code}")
print(f"Response Body: {e.response.text}")