Running IBM SPSS Statistics from an External Python Process

You can run Python programs utilizing the spss module 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.

To drive the IBM SPSS Statistics processor from a Python IDE, simply include an import spss statement in the IDE's code window, followed by a call to the spss.StartSPSS function. You can then call any of the functions in the spss module, just like with program blocks in command syntax jobs, but you do not need to wrap your Python code in BEGIN PROGRAM-END PROGRAM statements. Some initial configuration may be required as described in the following sections.

Windows Users

You can start IDLE (the default IDE provided with Python) for Python 2.7 or Python 3.4 from All Programs > IBM SPSS Statistics > Python 2.7 for IBM SPSS Statistics 26 > Python 2.7 IDLE(PythonGUI) or All Programs > IBM SPSS Statistics > Python 3.4 for IBM SPSS Statistics 26 > Python 3.4 IDLE(PythonGUI). This action starts IDLE from the Python location that is specified on the File Locations tab in the Options dialog. You can then use import spss to start driving the IBM SPSS Statistics processor.

If you do not use IDLE to drive IBM SPSS Statistics, then you must modify the Python search path to add the path to the spss module. You can add to the Python search path by modifying (or creating) the sitecustomize.py module for the installation of Python 2.7 or Python 3.4 that you want to use. The sitecustomize.py module, if it exists, is in the <PYTHON_HOME>\Lib\site-packages directory, where <PYTHON_HOME> is the installation location of Python 2.7 or Python 3.4; for example, C:\Python27\Lib\site-packages. If it does not exist then create it in that location.

For Python 2.7, add the following lines to sitecustomize.py:

import sys
sys.path.insert(0,r'<SPSS_HOME>\Python\Lib\site-packages')

For Python 3.4, add the following lines to sitecustomize.py:

import sys
sys.path.insert(0,r'<SPSS_HOME>\Python3\Lib\site-packages')

In the preceding expressions, <SPSS_HOME> is the installation location of IBM SPSS Statistics; for example, C:\Program Files\IBM\SPSS\Statistics\26.

Linux Users

The statisticspython script (for Python 2) or the statisticspython3 script (for Python 3), in the bin directory under the location where IBM SPSS Statistics is installed, starts the Python interpreter from the Python location that is specified on the File Locations tab in the Options dialog. You can then use import spss to start driving the IBM SPSS Statistics processor.

If you choose not to use this script, then you must modify the Python search path to add the path to the spss module. You can add to the Python search path by modifying (or creating) the sitecustomize.py module for the installation of Python 2.7 or Python 3.4 that you want to use. The sitecustomize.py module, if it exists, is in the <PYTHON_HOME>/lib/python2.7/site-packages or <PYTHON_HOME>/lib/python3.4/site-packages directory, where <PYTHON_HOME> is the installation location of Python 2.7 or Python 3.4. If it does not exist then create it in that location.

For Python 2.7, add the following lines to sitecustomize.py:

import sys
sys.path.insert(0,'<SPSS_HOME>/Python/lib/python2.7/site-packages')

For Python 3.4, add the following lines to sitecustomize.py:

import sys
sys.path.insert(0,'<SPSS_HOME>/Python3/lib/python3.4/site-packages')

In the preceding expression, <SPSS_HOME> is the installation location of IBM SPSS Statistics; for example, /opt/IBM/SPSS/Statistics/26.

You must also modify the LD_LIBRARY_PATH environment variable as follows:

export LD_LIBRARY_PATH=<PYTHON_HOME>/lib:<SPSS_HOME>/lib:$LD_LIBRARY_PATH

In the preceding expression, <PYTHON_HOME> is the location of the installation of Python 2.7 or Python 3.4 that you want to use. For reference, for the version of Python 2.7 that is installed with IBM SPSS Statistics, <PYTHON_HOME> is <SPSS_HOME>/Python. And, for Python 3.4, <PYTHON_HOME> is <SPSS_HOME>/Python3.

Mac Users

To drive the IBM SPSS Statistics processor from an external Python 2 or Python 3 process on Mac, start the Python2 for SPSS Statistics or Python3 for SPSS Statistics application, which are in the directory where IBM SPSS Statistics is installed. The applications start IDLE from the Python location that is specified on the File Locations tab in the Options dialog. You can then use import spss to start driving the IBM SPSS Statistics processor.

If you choose not to use the Python2 for SPSS Statistics or Python3 for SPSS Statistics application, then you must modify the Python search path to add the path to the spss module. You can add to the Python search path by modifying (or creating) the sitecustomize.py module for the installation of Python 2.7 or Python 3.4 that you want to use. The sitecustomize.py module, if it exists, is in the <PYTHON_HOME>/lib/python2.7/site-packages or <PYTHON_HOME>/lib/python3.4/site-packages directory, where <PYTHON_HOME> is the installation location of Python 2.7 or Python 3.4. If it does not exist then create it in that location.

For Python 2.7, add the following lines to sitecustomize.py:

import sys
sys.path.insert(0,'<INSTALLDIR>/Python/lib/python2.7/site-packages')

For Python 3.4, add the following lines to sitecustomize.py:

import sys
sys.path.insert(0,'<INSTALLDIR>/Python3/lib/python3.4/site-packages')

In the preceding expression, <INSTALLDIR> is the location of the IBM SPSS Statistics application bundle; for example, /Applications/IBM/SPSS/Statistics/26.

You must also modify environment variables as follows:

export
DYLD_LIBRARY_PATH=<SPSS_HOME>/lib:<SPSS_HOME>/Library/Frameworks/Sentinel.framework/Versions/A:
<SPSS_HOME>/Library/Frameworks/SuperPro.framework/Versions/A
export PYTHONHOME=<PYTHON_HOME>

In the preceding expression, <SPSS_HOME> is the location of the Contents folder in the IBM SPSS Statistics application bundle, and is given by <INSTALLDIR>/SPSSStatistics.app/Contents. <PYTHON_HOME> is the location of the installation of Python 2.7 or Python 3.4 that you want to use. For reference, for the version of Python 2.7 that is installed with IBM SPSS Statistics, <PYTHON_HOME> is <INSTALLDIR>/Python. And, for Python 3.4, <PYTHON_HOME> is <INSTALLDIR>/Python3.