Skip to main content

Sonar logo

SonarScanner for Python (pysonar)

SonarScanner for Python runs SonarQube analysis from Python projects. It is published on PyPI as pysonar; use the installation and configuration instructions below to run an analysis.

To learn more about Sonar products, visit the Sonar website.

Requirements

  • SonarQube v10.6 or above
  • Python 3.9 or above

Installation

Install with pip:

pip install pysonar

Usage

Once installed, the pysonar scanner can be run from the command line to perform an analysis. It assumes a running SonarQube server or a project configured on SonarCloud.

Setting up analysis properties

In order for the analysis to run, analysis properties need to be defined. There are multiple ways of providing these properties, described below in descending order of priority:

  1. Through CLI arguments to the pysonar command
  2. Environment variables for individual properties (e.g. SONAR_TOKEN, SONAR_VERBOSE, SONAR_HOST_URL, ...)
  3. Generic environment variable SONAR_SCANNER_JSON_PARAMS
  4. Under the [tool.sonar] key of the pyproject.toml file
  5. In a dedicated sonar-project.properties file
  6. Through common properties extracted from the pyproject.toml

Through CLI arguments

Analysis properties can be provided as CLI arguments to the pysonar command. They can be provided in a similar way as when running the SonarScanner CLI directly (see documentation). This means that analysis properties provided that way should be prepended with -D, for instance:

$ pysonar -Dsonar.token=myAuthenticationToken 

You can use all the arguments allowed by SonarScanner. For more information on SonarScanner please refer to the SonarScanner documentation.

Additionally, some common properties can be provided using a shorter alias, such as:

pysonar --token "MyToken"

See CLI_ARGS for more details.

With a pyproject.toml file

Inside a pyproject.toml, Sonar analysis properties can be defined under the tool.sonar table.

[tool.sonar]
# must be unique in a given SonarQube/SonarCloud instance
projectKey=my:project

# --- optional properties ---
# defaults to project key
#projectName=My project
# defaults to 'not provided'
#projectVersion=1.0
 
# Path is relative to the pyproject.toml file. Defaults to .
#sources=.
 
# Encoding of the source code. Default is default system encoding
#sourceEncoding=UTF-8

The configuration parameters can be found in the SonarQube documentation.

In the pyproject.toml file the prefix sonar. for parameter keys should be omitted. For example, sonar.scm.provider in the documentation will become scm.provider in the pyproject.toml file.

Properties in pyproject.toml files are expected to be provided in camel case. However, kebab case is also accepted:

[tool.sonar]
project-key=My Project key # valid alias for projectKey

By default, the scanner will expect the pyproject.toml file to be present in the current directory. However, its path can be provided manually through the toml-path CLI argument as well as through the sonar.projectBaseDir argument. The --toml-path argument accepts either the path to the pyproject.toml file itself or to the folder containing it. For instance:

pysonar --toml-path "path/to/pyproject.toml"

Or equivalently:

pysonar --toml-path "path/to"

Or:

pysonar --sonar-project-base-dir "path/to/projectBaseDir"

Or:

pysonar -Dsonar.projectBaseDir="path/to/projectBaseDir"

Through project properties extracted from the pyproject.toml

When a pyproject.toml file is available, the scanner can deduce analysis properties from the project configuration. This is currently supported only for projects using poetry.

With a sonar-project.properties file

Exactly like SonarScanner, the analysis can also be configured with a sonar-project.properties file:

# must be unique in a given SonarQube/SonarCloud instance
sonar.projectKey=my:project

# --- optional properties ---

# defaults to project key
#sonar.projectName=My project
# defaults to 'not provided'
#sonar.projectVersion=1.0
 
# Path is relative to the sonar-project.properties file. Defaults to .
#sonar.sources=.
 
# Encoding of the source code. Default is default system encoding
#sonar.sourceEncoding=UTF-8

Through environment variables

It is also possible to configure the scanner through environment variables:

$ export SONAR_HOST_URL="http://localhost:9000"
$ pysonar 

See the SonarScanner documentation for more information.

Feedback

For feedback and issues regarding pysonar, do not hesitate to contact us through our Community.

Installation from testPyPI

To install the latest pre-released version of Sonar Scanner Python. Execute the following command:

pip install --index-url https://test.pypi.org/simple/ --extra-index-url https://pypi.org/simple/ pysonar

License

Copyright 2011-2026 SonarSource.

Licensed under the GNU Lesser General Public License, Version 3.0

Metadata

Release files for pysonar 1.10.0.5888

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for pysonar 1.10.0.5888
File Size Uploaded Provenance Actions
pysonar-1.10.0.5888.tar.gz 34.1 kB Without provenance Details for pysonar-1.10.0.5888.tar.gz

Built distribution (wheel)

Table of built distributions (wheels) for pysonar 1.10.0.5888
File Interpreter ABI Platform Actions
pysonar-1.10.0.5888-py3-none-any.whl Python 3 none any Details for pysonar-1.10.0.5888-py3-none-any.whl

Total release size: 85.2 kB

Release files / pysonar-1.10.0.5888.tar.gz

Download URL pysonar-1.10.0.5888.tar.gz
Size 34.1 kB
Tags Source
SHA-256 checksum
How to use checksums
91a0d1106a8cd5b5452a98f777d0e72b3c021736a19fa685c15109f536dfc000
BLAKE2b-256 checksum
How to use checksums
03d332a7bcda552ee2f432052fc5e3af3c54b4b536627df00af216845c74d91f
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/7.0.0 CPython/3.13.16

Release files / pysonar-1.10.0.5888-py3-none-any.whl

Download URL pysonar-1.10.0.5888-py3-none-any.whl
Size 51.1 kB
Tags Python 3
SHA-256 checksum
How to use checksums
6c400ee4c65964a0d63171476a324b95fa16ebfec1d26f71e9e6a08f56765608
BLAKE2b-256 checksum
How to use checksums
e04edfe8c4698b492160c7a0a9f63c20020572df7c73a9671524b8b2b6d5c95f
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/7.0.0 CPython/3.13.16

Security for pysonar 1.10.0.5888

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

No provenance found

None of the files in this release were uploaded with provenance metadata.

A note on package security

PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.

To learn more about protecting yourself from malicious actors, visit our package security documentation.

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page