Set Up Python API for Polyspace
You can manage Polyspace® Test™ workflows entirely at the command line by using the Python® API for Polyspace. You can create and manage projects, configure projects based on your toolchain and other requirements, and build and run tests for C/C++ code. After you run the tests, you can save the results in a Polyspace Test results file or an HTML or XML report.
This topic shows how to set up the Python API for Polyspace.
Supported Python Versions
To import the polyspace modules in a Python shell, you need one of these Python versions:
3.9.x
3.10.x
3.11.x
3.12.x
3.13.x
Make sure you have installed a supported Python version. You can see your installed Python version by typing:
python --version
python3 --version
Set Up Import of Polyspace Modules
The Python API for Polyspace consists of modules such as polyspace.project and polyspace.test. To author and run tests using these modules, you must install the polyspace package first.
Install the polyspace package from the location <polyspaceroot>\polyspace\api\python by running pip install at the command line (requires pip versions 21.3 or greater). Here, <polyspaceroot> is the Polyspace installation folder, for instance, C:\Program Files\Polyspace\R2026a.
For instance, you can install the package in a virtual environment by following these steps:
Create a virtual environment in a folder
<rootFolder>. At the command line, navigate to this folder and enter:Or:python -m venv venvpython3 -m venv venvActivate the virtual environment by entering the following in Windows® or its equivalent in Linux®:
You see.\venv\Scripts\activate(venv)on the left of the command prompt to indicate that you are in the virtual environment. You can leave the virtual environment by enteringdeactivateon this prompt.Install the Polyspace package using
pip install:Here,pip install "<polyspaceroot>\polyspace\api\python"<polyspaceroot>is the Polyspace installation folder, for instance,C:\Program Files\Polyspace\R2026a.You can check if the package is installed in the virtual environment by running
pip listor uninstall the package by runningpip uninstall polyspace(orpip uninstall polyspace==<version_number>to uninstall a specific version).Test if the package installation worked by checking if you can import the
polyspace.projectmodule without errors.For instance:
If you are running Python commands at the command line:
With your virtual environment activated, start the Python interactive interpreter by entering the following:
Or:pythonpython3Once you enter this Python interactive interpreter mode, run the following command:
To exit this mode and re-enter the command line, enter:import polyspace.projectexit()
If you are running Python scripts in Visual Studio Code using Python and Python Debugger extensions, you can specify a Python interpreter using these steps:
Select View > Command Palette and choose Python: Select Interpreter.
Select Enter interpreter path and then select Find to navigate to
<rootFolder>\venv\Scripts\python.In a Python script, enter the following command:
import polyspace.projectDebug or run the script using the method you typically use. For instance, for debugging, select View > Command Palette and choose Python Debugger: Debug Python File.
Troubleshoot Polyspace Module Import
If you see the following error when importing the polyspace.project module:
ModuleNotFoundError: No module named 'polyspace'Make sure you have installed a supported Python version. For more information, see Supported Python Versions.
Make sure you are not using the MSYS2 version of Python that comes with a MinGW installation.
Make sure your
pipversion is 21.3 or greater. If you need to use a lower version, use the option--use-feature=in-tree-buildwhen installing thepolyspacepackage.
If you see this error, try reinstalling a supported Python and pip version and retry the module import Python commands.