Running Python scripts and Python programs

Both Python scripts and Python programs can be run from within IBM® SPSS® Statistics or from an external Python process, such as a Python IDE or the Python interpreter.

Python scripts

Python script run from IBM SPSS Statistics. You can run a Python script from Utilities > Run Python script or from the Python script editor, which is launched when opening a Python file (*.py) from File > Open. Scripts run from the Python editor that is launched from IBM SPSS Statistics operate on the IBM SPSS Statistics client that launched the editor. This allows you to debug your Python code from a Python editor.

Python script run from an external Python process. You can run a Python script from any external Python process, such as a Python IDE that is not launched from IBM SPSS Statistics, or the Python interpreter. The script will attempt to connect to an existing IBM SPSS Statistics client. If more than one client is found, a connection is made to the most recently launched one. If an existing client is not found, the Python script starts up a new instance of the IBM SPSS Statistics client. By default, the Data editor and Workbook tab are invisible for the new client. You can choose to make them visible or work in invisible mode with datasets and output documents.

Python programs

Python program run from command syntax. You can run a Python program by embedding Python code within a BEGIN PROGRAM-END PROGRAM block in command syntax. The command syntax can be run from the IBM SPSS Statistics client.

Python program run from an external Python process. You can run a Python program from any external Python process, such as a Python IDE or the Python interpreter. In this mode, the Python program starts up a new instance of the IBM SPSS Statistics processor without an associated instance of the IBM SPSS Statistics client. You can use this mode to debug your Python programs using the Python IDE of your choice.

Invoking Python scripts from Python programs (and vice-versa)

Python script run from Python program. You can run a Python script from a Python program by importing the Python module containing the script and calling the function in the module that implements the script. You can also call Python script methods directly from within a Python program. .

Python program run from Python script. Python scripts can run command syntax, which means they can run command syntax containing Python programs.

Limitations and warnings

  • Running a Python program from the Python editor launched by IBM SPSS Statistics will start up a new instance of the IBM SPSS Statistics processor and will not interact with the instance of IBM SPSS Statistics that launched the editor.
  • Python programs are not intended to be run from Utilities > Run script.
  • The interfaces exposed by the spss module cannot be used in a Python script.