{"id":8523,"library":"pysonar","title":"Sonar Scanner for Python (pysonar)","description":"pysonar is a command-line tool developed by SonarSource for static code analysis of Python projects with SonarQube or SonarCloud. It acts as a wrapper around the SonarScanner CLI, simplifying configuration and execution for Python ecosystems. It is actively maintained with frequent releases, currently at version 1.4.0.4676, and supports configuration via `pyproject.toml` or `sonar-project.properties` files.","status":"active","version":"1.4.0.4676","language":"python","source_language":"en","source_url":"https://github.com/SonarSource/sonar-scanner-python","tags":["static analysis","code quality","sonar","sonarqube","security","cli"],"install":[{"cmd":"pip install pysonar","lang":"bash","label":"Install pysonar"}],"dependencies":[{"reason":"Runtime environment.","package":"Python","optional":false},{"reason":"Requires SonarQube Server v10.6 or higher, or SonarCloud, for analysis reporting.","package":"SonarQube Server","optional":false}],"imports":[{"note":"pysonar is primarily a command-line interface (CLI) tool and is not intended to be imported as a Python library for direct programmatic use.","wrong":"import pysonar","symbol":"pysonar","correct":"pysonar --help"}],"quickstart":{"code":"# 1. Install pysonar\npip install pysonar\n\n# 2. Configure your project (e.g., in pyproject.toml in your project root)\n#    Replace <your-project-key> with your SonarQube project key.\n#    For SonarCloud, also uncomment and set 'organization'.\n#    For SonarQube Server, ensure host.url is set if not default.\n#\n#  [tool.sonar]\n#  project-key = \"<your-project-key>\"\n#  # host.url = \"https://sonarqube.example.com\"\n#  # organization = \"<your-organization-key>\" \n\n# 3. Run the analysis\n#    Ensure SONAR_TOKEN is set in your environment with a valid SonarQube/SonarCloud token.\n#    Example: export SONAR_TOKEN=\"your_sonar_token_here\"\nimport os\nsonar_token = os.environ.get('SONAR_TOKEN', '')\nif not sonar_token:\n    print(\"Error: SONAR_TOKEN environment variable not set. Please set it before running pysonar.\")\nelse:\n    print(\"Running SonarQube analysis...\")\n    # The actual command would be run in your shell or CI/CD pipeline\n    # For demonstration, we'll print it. In a real scenario, you'd use subprocess.run()\n    print(f\"Executing: pysonar --token {sonar_token}\")\n    # Example of actual command to run (requires `pysonar` to be in PATH):\n    # import subprocess\n    # result = subprocess.run([\"pysonar\", \"--token\", sonar_token], capture_output=True, text=True)\n    # print(result.stdout)\n    # if result.stderr: print(result.stderr)\n    # if result.returncode != 0: print(\"Analysis failed!\")","lang":"python","description":"To quickly get started, install `pysonar`, configure your project (ideally via a `[tool.sonar]` section in `pyproject.toml`), and then run the `pysonar` command with your SonarQube or SonarCloud authentication token. The token can be passed via command-line argument `--token` or an environment variable `SONAR_TOKEN`."},"warnings":[{"fix":"Uninstall `pysonar-scanner` and install `pysonar`: `pip uninstall pysonar-scanner && pip install pysonar`.","message":"The `pysonar-scanner` PyPI package is deprecated in favor of `pysonar`. Users should migrate to `pysonar` for all new projects and existing projects where possible. The deprecated package is no longer actively maintained and may break.","severity":"breaking","affected_versions":">=0.3.0.2016 for pysonar-scanner, all versions of pysonar"},{"fix":"Ensure your SonarQube server is version 10.6+ or use SonarCloud. If not possible, consider using the `pysonar-scanner` (deprecated) for older SonarQube versions, or upgrade pysonar to at least 1.0.1.","message":"pysonar requires SonarQube Server v10.6 or higher, or SonarCloud. Older versions of pysonar (before 1.0.1) incorrectly stated compatibility with SonarQube v9.9. If you are on an older SonarQube server, you might need to use the deprecated `pysonar-scanner` or upgrade your SonarQube instance.","severity":"gotcha","affected_versions":"<1.0.1"},{"fix":"Instead of `sonar.projectKey = \"mykey\"`, use `projectKey = \"mykey\"` or `project-key = \"mykey\"` in `pyproject.toml`.","message":"When defining SonarQube analysis properties in a `pyproject.toml` file under `[tool.sonar]`, the `sonar.` prefix for property keys should be omitted. Property keys are generally camelCase (e.g., `projectKey`), although kebab-case is also accepted (e.g., `project-key`).","severity":"gotcha","affected_versions":"All versions"},{"fix":"When debugging complex analysis issues, consult the official SonarScanner CLI documentation in addition to pysonar's specific guidance.","message":"pysonar is a wrapper around the SonarScanner CLI. While it simplifies Python project analysis, it still fundamentally relies on the underlying SonarScanner CLI. This means some deep troubleshooting might involve understanding the generic SonarScanner CLI behavior.","severity":"gotcha","affected_versions":"All versions"}],"env_vars":null,"search_vec":"'1.4.0.4676':53 'act':28 'activ':45 'analysi':19,63 'around':32 'cli':35,69 'code':18,64 'command':10 'command-lin':9 'configur':37,56 'current':50 'develop':13 'ecosystem':42 'execut':39 'file':61 'frequent':48 'line':11 'maintain':46 'project':22 'pyproject.toml':58 'pysonar':5,6 'python':4,21,41 'qualiti':65 'releas':49 'scanner':2 'secur':68 'simplifi':36 'sonar':1,66 'sonar-project.properties':60 'sonarcloud':26 'sonarqub':24,67 'sonarscann':34 'sonarsourc':15 'static':17,62 'support':55 'tool':12 'version':52 'via':57 'wrapper':31","created_at":"2026-04-16T17:02:38.433958+00:00","updated_at":"2026-04-16T17:02:38.433958+00:00","problems":{"verify_error":"no import statement found"},"ecosystem":"pypi","meta_description":null,"install_score":null,"quickstart_score":null,"quickstart_tag":null,"pypi_latest":"1.6.0.4905","cli_name":"pysonar","cli_version":"ERROR: Unexpected argument: --version","type":"library","homepage":"https://www.sonarsource.com/","github":"https://github.com/SonarSource/sonar-scanner-python","docs":null,"changelog":null,"pypi":"https://pypi.org/project/pysonar/","npm":null,"openapi_spec":null,"status_page":null,"smithery":null,"categories":["devops","testing"],"base_url":null,"auth_type":null,"provenance":{"verified_status":"skip","verified_at":"2026-06-28","last_verified":"2026-06-28","next_check":"2026-07-05","install_tag":null}}