Running Python code with Python

Python 2 is no longer installed with IBM SPSS Statistics and is not supported in IBM SPSS Statistics version 28 and above.

Python 3.9.1 is installed with SPSS version 28; Python 3.10.4 is installed with SPSS version 29.

For release 28 and higher, the IBM® SPSS® Statistics - Integration Plug-in for Python supports code written in Python 3.9.1. All functions and classes available with the Integration Plug-in for Python are supported in Python 3.9.1.

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

Accessing the Python 3 runtime environment

Using the User Interface

  1. In Windows, click “Python3 for SPSS Statistics (GUI)”.
  2. On Mac, by click “Python3 for SPSS Statistics”.

From a command prompt or terminal window

  1. In Windows, by selecting statisticspython3.bat located in the IBM SPSS Statistics installation path.
  2. On Mac, by selecting the statisticspython3 script located in the application bundle’s /bin folder.

Running Python 3 in SPSS

From command syntax

  • In a syntax window, place the Python code in a BEGIN PROGRAM PYTHON3 - END PROGRAM block.
  • Using the SCRIPT command, specify PYTHONVERSION=3
  • When developing an extension command, specify LanguageVersion="3" along with Language="Python" in the Command element of the extensions’s XML specification.

From the User Interface

  • Select File > New > Script or File > Open > Script.
  • Alternatively, use Utilities > Run Script.

Using an external Python 3 distribution

To use an external Python 3 distribution, set the home path of the external distribution in the Python 3 home directory field (Edit > Options > File Locations > Python 3 Location > Other installation of Python 3). For IBM SPSS Statistics Server, update the Python 3 home path in the spssdxcfg.ini file's [Python3] section. The file is located in the SPSS Statistics installation path (bin folder on UNIX). For example:

[Python3]

HOME=

LIB_NAME=InvokePython39

Using Python 3 third-party packages

To manually install third-party Python 3 packages, complete the following steps:

  • Open a command line instance
  • Go to the SPSS Statistics installation path (bin folder on UNIX).
  • Run the statisticspython3 script using -m pip install <package_name1> <package_name2> to install packages. For example, enter the following command to install the numpy and scipy packages.

On Windows: statisticspython3.bat -m pip install numpy scipy.

On UNIX-based systems: statisticspython3 -m pip install numpy scipy

Known issues

  • Python scripting and com scripting will not work if the application is in Workbook mode (Edit > Options... > General > Application Mode).
  • The Python runtime environment will not work after you uninstall IBM SPSS Statistics and reinstall to a different path. To resolve the issue you must remove the virtual Python environment (Python39 directory).

On Windows: %APPDATA%\IBM\SPSS Statistics\28\Python39.

On macOS: ~/Library/Application Support/IBM/SPSS Statistics/28/Python39.