主要内容

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
or
python3 --version
at the command line.

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:

  1. Create a virtual environment in a folder <rootFolder>. At the command line, navigate to this folder and enter:

    python -m venv venv
    Or:
    python3 -m venv venv

  2. Activate the virtual environment by entering the following in Windows® or its equivalent in Linux®:

    .\venv\Scripts\activate
    You see (venv) on the left of the command prompt to indicate that you are in the virtual environment. You can leave the virtual environment by entering deactivate on this prompt.

  3. Install the Polyspace package using pip install:

    pip install "<polyspaceroot>\polyspace\api\python"
    Here, <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 list or uninstall the package by running pip uninstall polyspace (or pip uninstall polyspace==<version_number> to uninstall a specific version).

  4. Test if the package installation worked by checking if you can import the polyspace.project module without errors.

    For instance:

    • If you are running Python commands at the command line:

      1. With your virtual environment activated, start the Python interactive interpreter by entering the following:

        python
        Or:
        python3

      2. Once you enter this Python interactive interpreter mode, run the following command:

        import polyspace.project
        To exit this mode and re-enter the command line, enter:
        exit()

    • 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:

      1. Select View > Command Palette and choose Python: Select Interpreter.

      2. Select Enter interpreter path and then select Find to navigate to <rootFolder>\venv\Scripts\python.

      3. In a Python script, enter the following command:

        import polyspace.project

      4. Debug 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'
Check the following:

  • 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 pip version is 21.3 or greater. If you need to use a lower version, use the option --use-feature=in-tree-build when installing the polyspace package.

If you see this error, try reinstalling a supported Python and pip version and retry the module import Python commands.

See Also

Topics