Running Python Code with Python 2 or Python 3

For release 24 and higher, the IBM® SPSS® Statistics - Integration Plug-in for Python supports code that is written in either Python 2 or Python 3. All functions and classes that are available with the Integration Plug-in for Python are supported in both Python 2 and Python 3. In addition, distributions of both Python 2 and Python 3 are installed with the Integration Plug-in for Python.

Choosing to use the Python 2 or the Python 3 processor is done on a case by case basis. In cases where a default is possible, such as in command syntax, the default is to use the Python 2 processor. In all cases, you can explicitly specify to use the Python 3 processor.

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 2 or Python 3 in Command Syntax

  • The Python 2 processor is used for program blocks that are specified by BEGIN PROGRAM-END PROGRAM, BEGIN PROGRAM PYTHON-END PROGRAM, or BEGIN PROGRAM PYTHON2-END PROGRAM. To use the Python 3 processor for Python code within a program block, use BEGIN PROGRAM PYTHON3-END PROGRAM.
  • By default, Python scripts that are run from the SCRIPT command are run with the Python 2 processor. To run a script that uses the Python 3 processor, use PYTHONVERSION=3 on the SCRIPT command.
  • By default, extension commands that are implemented in Python use the Python 2 processor. 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 2 or Python 3 in the User Interface

  • Scripts that are run from the Python script editor, opened from File>New>Script or File>Open>Script, run in the Python version that was selected on the Script menu or Open Script dialog.
  • Scripts that are run from Utilities>Run Script run in the Python version that was selected on the Run Script dialog.
  • You can set the default Python version for both the Run Script and Open Script dialogs from the Default script language setting on the Scripts tab of the Options dialog. Python scripts that are opened from the Welcome dialog or Recently Used Files list open in the Python 2 script editor unless the Default script language is Python 3.
  • For autoscripts, you explicitly specify on the Scripts tab of the Options dialog whether to run an autoscript in Python 2 or Python 3.