4.11. Python Configuration

Sonargraph supports Python version 3 and higher. To enable the support Sonargraph must know the location of the executable for the Python interpreter. You can configure that in the "Python Preferences" "section of the Sonargraph preferences dialog. We also assume that you would use virtual environments for managing project specific dependencies. In that case you should configure the Python interpreter of your virtual environment in the setting dialog brought up by "System/Configure". In any case Sonargraph will ensure that your interpreter supports at least Python 3.

Since Python is a dynamic language many dependencies will not be detectable by Sonargraph - everything is an object and typing information is rarely available. Nevertheless the model will still contain the most relevant dependencies (e.g. object creation, inheritance, function calls, member access etc.) so that the result is good enough to analyze dependencies and enforce architectural constraints. Please be sure to read the section about Sonargraph's Python model in the next chapter.

To analyze a Python system with Sonargraph you must execute the following steps:

  • Create a new software system by using "File / New / New System..."

  • Add a Python module by selecting "File / New / Module / New Python Module...". Usually Python systems only contain a single module.

  • Add the root directory for your Python project by right clicking on the module you created in the previous step and select "New Root Directory...". If you have more than one source root directory you can add several.

  • If you project uses a virtual environment please configure the Python 3 interpreter of this virtual environment via the "System / Configure..." dialog.

  • Save your newly created system.

  • Start the parser by clicking on the "refresh" icon (top left icon in the tool bar). The first parser run will always take longer since we have to parse all the directly and indirectly imported files from the Python library.

  • Now you should have a model and you can browse dependencies, metrics and anything else that is contained in the model.