Install & Compatibility
Where this runs
tested against v2.0.15 · pip install
no network on importno background threads
Install × environment matrix
Each cell = how many times install + import succeeded across repeated harness runs. Partial = flaky.
glibc = Debian/Ubuntu slim · musl = Alpine Linux
muslpy 3.10–3.910 runs
installs and imports cleanly · install 0.0s · import 0.601s · 22.2MB
glibcpy 3.10–3.910 runs
installs and imports cleanly · install 5.2s · import 0.562s · 23MB
24MB installed
● package 24MB
Code
Verified usage
Verified import paths — ran on the pinned version, not inferred.
GerritRestAPI
✓ from pygerrit2 import GerritRestAPI
HTTPBasicAuth
✓ from pygerrit2 import HTTPBasicAuth
✗ from requests.auth import HTTPDigestAuth
Gerrit 2.14 and later primarily use HTTP Basic Auth. HTTP Digest Auth was deprecated.
HTTPBasicAuthFromNetrc
✓ from pygerrit2 import HTTPBasicAuthFromNetrc
✗ from pygerrit2.rest.auth import HTTPDigestAuthFromNetrc
Similar to HTTPBasicAuth, prefer Basic Auth for modern Gerrit.
This example demonstrates how to initialize `GerritRestAPI` with `HTTPBasicAuth` using credentials from environment variables and fetch a list of open changes. It also shows how to retrieve information about a specific project. Remember that the HTTP password is distinct from the SSH password.
import os
from pygerrit2 import GerritRestAPI, HTTPBasicAuth
GERRIT_URL = os.environ.get('GERRIT_URL', 'http://localhost:8080')
GERRIT_USERNAME = os.environ.get('GERRIT_USERNAME', 'admin')
GERRIT_HTTP_PASSWORD = os.environ.get('GERRIT_HTTP_PASSWORD', 'secret') # This is NOT your SSH password
if not all([GERRIT_URL, GERRIT_USERNAME, GERRIT_HTTP_PASSWORD]):
print("Please set GERRIT_URL, GERRIT_USERNAME, and GERRIT_HTTP_PASSWORD environment variables.")
exit(1)
try:
auth = HTTPBasicAuth(GERRIT_USERNAME, GERRIT_HTTP_PASSWORD)
rest = GerritRestAPI(url=GERRIT_URL, auth=auth)
# Get open changes owned by the authenticated user
changes = rest.get("/changes/?q=owner:self+status:open")
print(f"Found {len(changes)} open changes for {GERRIT_USERNAME}:")
for change in changes:
print(f" Change ID: {change['id']}, Subject: {change['subject']}")
# Example of getting a specific project
project_name = "All-Projects"
project_info = rest.get(f"/projects/{project_name}")
print(f"\nProject '{project_name}': {project_info['description']}")
except Exception as e:
print(f"An error occurred: {e}")
Errors
Common errors & fixes
requests.exceptions.HTTPError: 401 Client Error: Unauthorized for url:...
This typically occurs due to incorrect authentication. Common reasons include using the wrong authentication type (e.g., `HTTPDigestAuth` with Gerrit 2.14+), providing incorrect HTTP credentials, or a mismatch between the provided username and the associated HTTP password.
fixVerify that you are using `pygerrit2.HTTPBasicAuth` for modern Gerrit versions (2.14+). Double-check the username and the specific HTTP password obtained from your Gerrit user settings (not your SSH password).
requests.exceptions.ConnectionError: ('Connection aborted.', ConnectionResetError(104, 'Connection reset by peer'))
This error indicates a network-level issue where the connection to the Gerrit server was unexpectedly closed or could not be established. This can be due to transient network problems, server overload, or an incorrect `GERRIT_URL`.
fixCheck your `GERRIT_URL` for correctness and ensure network connectivity to the Gerrit server. For production environments, consider implementing retry logic with a library like `backoff` to handle transient connection issues gracefully.
requests.exceptions.HTTPError: 400 Client Error: Bad Request for url:...
A `400 Bad Request` usually means the server understood your request but couldn't process it due to invalid syntax or parameters in your request body or URL. This is common when making POST/PUT requests with malformed JSON data.
fixCarefully review the Gerrit REST API documentation for the specific endpoint you are calling, paying close attention to the required JSON input format and URL parameters. Ensure your Python dictionary is correctly serialized to JSON and sent with the appropriate `Content-Type` header (though `pygerrit2` often handles this).
Upgrade
Version history
2.0.15latest on PyPI · released Jun 3, 2021
Audit
Dependencies
requestsrequiredCore HTTP client library for making API calls.