Registry / devops / python-sonarqube-api

python-sonarqube-api

JSON →
library2.0.5pypypiunverified

python-sonarqube-api is a Python wrapper for the SonarQube and SonarCloud API, providing a convenient interface to interact with SonarQube Community, Enterprise, and SonarCloud instances. It supports Python 2.7 and 3.3+ (though modern usage typically implies Python 3.6+). The library is actively maintained, with version 2.0.5 currently available, facilitating integration of SonarQube's static analysis capabilities into Python applications and CI/CD pipelines.

pip install --upgrade python-sonarqube-api
INSTALL
IMPORT
SIG · PYTHON-SONARQUBE-A
P
python-sonarqube-api
devopspythonv2.0.5
Install
2.1s avg
Import
600ms
Disk
20MB
Pass rate
10/ 10
Env Coverage10 / 10
glibc
3.93.13
musl
3.93.13
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
musl
py 3.103.920 runs
installs and imports cleanly · install 0.0s · import 0.634s · 21.5MB
glibc
py 3.103.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.
fix
For 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.
fix
Iterate 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.
fix
Generate 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.
fix
Ensure 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.

Agent activity
8 hits · last 30 days
node
8
Resources
python-sonarqube-api — pip install python-sonarqube-api · libregistry