Running Python code with Python

For release 27 and higher, the IBM® SPSS® Statistics - Integration Plug-in for Python supports code that is written in Python 3. All functions and classes that are available with the Integration Plug-in for Python are supported in Python 3. In addition, a Python 3 distribution is installed with the Integration Plug-in for Python.

Important: Use of the Python 3 processor requires that IBM SPSS Statistics is in Unicode mode. For more information, see SET command, UNICODE subcommand.

Python 3 in command syntax

  • To use the Python 3 processor for Python code within a program block, use BEGIN PROGRAM PYTHON3-END PROGRAM.
  • To run a script that uses the Python 3 processor, use PYTHONVERSION=3 on the SCRIPT command.
  • To use the Python 3 processor for an extension command, specify LanguageVersion="3" along with Language="Python" in the Command element of the XML specification for the extension command.

Python 3 in the user interface

  • Scripts that are run from the Python script editor, opened from File > New > Script or File > Open > Script.
  • Scripts that are run from Utilities > Run Script run in the Python 3 version that was selected on the Run Script dialog.

Python 2 support

Starting with IBM SPSS Statistics 27, the Python 2.x distribution is no longer installed with the product. Python 3.x is installed with the product, and is the recommended distribution. All installed Python 2.x scripts and available Python 2.x extensions have been updated to work with Python 3.x.

Users with Python 2.x scripts/extensions will need to manually install a Python 2.x distribution in order to continue working with the version 2.x scripts/extensions. The following error message occurs when a Python 2.x script/extension is used in IBM SPSS Statistics:
Error # 8500. Command name: BEGIN PROGRAM
Python 2 is no longer installed with IBM SPSS Statistics.
Perform the following steps to continue using Python 2.x scripts/extensions:
  1. Download and install a Python 2.7.x distribution.
  2. Start SPSS Statistics and enable the Installation of Python 2.7 setting via Options > File Locations. After enabling the setting, specify the Python 2.7 directory location in the Python 2.7 home directory field (you can click Browse... to navigate to the appropriate directory).
    Important:
    • To use the Python 2 processor for Python code within a program block, use BEGIN PROGRAM PYTHON2-END PROGRAM.
    • To run a script that uses the Python 2 processor, use PYTHONVERSION=2 on the SCRIPT command.
    • To use the Python 2 processor for an extension command, specify LanguageVersion="2" along with Language="Python" in the Command element of the XML specification for the extension command.