Install & Compatibility
Where this runs
tested against v2.0.5 · 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.920 runs
installs and imports cleanly · install 0.0s · import 0.634s · 21.5MB
glibcpy 3.10–3.920 runs
installs and imports cleanly · install 2.1s · import 0.565s · 22MB
20MB installed
● package 20MB
Code
Verified usage
Verified import paths — ran on the pinned version, not inferred.
SonarQubeClient
✓ from sonarqube import SonarQubeClient
SonarCloudClient
✓ from sonarqube import SonarCloudClient
SonarEnterpriseClient
✓ from sonarqube import SonarEnterpriseClient
SonarAPIHandler
✓ from sonarqube import SonarQubeClient
✗ from sonarqube_api import SonarAPIHandler
SonarAPIHandler was a class in an older/different, similarly named package (kako-nawao/python-sonarqube-api). The current official package uses SonarQubeClient, SonarCloudClient, or SonarEnterpriseClient from the 'sonarqube' top-level module.
This quickstart demonstrates how to connect to a SonarQube instance using a token and retrieve a list of projects. It uses environment variables for sensitive connection details. For SonarCloud, `SonarCloudClient` should be used with `sonarcloud_url` instead of `sonarqube_url`.
import os
from sonarqube import SonarQubeClient
# Configure connection details using environment variables for security
SONARQUBE_URL = os.environ.get('SONARQUBE_URL', 'http://localhost:9000')
SONARQUBE_TOKEN = os.environ.get('SONARQUBE_TOKEN', 'YOUR_SONARQUBE_TOKEN') # Or use user/password
# Initialize the SonarQube client
try:
client = SonarQubeClient(sonarqube_url=SONARQUBE_URL, token=SONARQUBE_TOKEN)
# Example: Fetch all projects
print(f"Connected to SonarQube at {SONARQUBE_URL}")
print("Fetching projects...")
projects = list(client.projects.search_projects())
if projects:
print(f"Found {len(projects)} projects:")
for project in projects[:3]: # Print first 3 projects
print(f" - {project.get('name')} (Key: {project.get('key')})")
else:
print("No projects found or accessible.")
except Exception as e:
print(f"An error occurred: {e}")
print("Please ensure SonarQube is running and accessible, and your token/credentials are correct.")
print("For SonarCloud, use SonarCloudClient and 'sonarcloud_url' parameter.")
Debug
Known issues
breakingSonarQube API changes in versions 9.7.1 and later removed the 'Statistics.edition' field from the `/api/system/info` endpoint.fixFor licensed SonarQube instances, use `/api/editions/show_license` to retrieve edition details. This field is intentionally removed for Community Edition.
affects: SonarQube 9.7.1+
gotchaSonarQube API methods that return large datasets are often paginated by the server. The `python-sonarqube-api` client methods frequently return generators to handle this efficiently.fixIterate directly over the returned generator or convert it to a list (e.g., `list(client.projects.search_projects())`) if you need all results immediately. Be mindful of memory consumption for very large datasets.
affects: All versions
gotchaToken-based authentication is the recommended and more secure method for programmatic access to SonarQube/SonarCloud APIs over username/password.fixGenerate an authentication token within your SonarQube/SonarCloud user profile and use it for `token` parameter when initializing client classes (e.g., `SonarQubeClient(token='...')`).
affects: All versions
gotchaDifferent client classes exist for different SonarQube environments: `SonarQubeClient` for SonarQube Community/Enterprise, `SonarCloudClient` for SonarCloud, and `SonarEnterpriseClient` specifically for SonarQube Enterprise Edition.fixEnsure you are using the correct client class for your target SonarQube environment and passing the appropriate URL parameter (`sonarqube_url` or `sonarcloud_url`).
affects: All versions
Upgrade
Version history
2.0.5latest on PyPI · released Aug 4, 2024
Audit
Dependencies
No dependency data recorded yet.