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 Script or from the Python script editor which is launched when opening a Python file (.py) from File>Open>Script. 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 Viewer 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 or from the IBM SPSS Statistics Batch Facility--a separate executable provided with IBM SPSS Statistics Server.

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. These features are not available when running a Python program from an external Python process or when running a Python program from the IBM SPSS Statistics Batch Facility (available with IBM SPSS Statistics Server).

Python Autoscript Triggered from Python Program. A Python script specified as an autoscript will be triggered when a Python program executes the procedure containing the output item associated with the autoscript. For example, you associate an autoscript with the Descriptive Statistics table generated by the Descriptives procedure. You then run a Python program that executes the Descriptives procedure. The Python autoscript will be executed.

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.
  • Python programs cannot be run as autoscripts.
  • The interfaces exposed by the spss module cannot be used in a Python script.