SPSS Statistics

SPSS Statistics

Your hub for statistical analysis, data management, and data documentation. Connect, learn, and share with your peers! 

 View Only
Expand all | Collapse all

How to install/import SpssClient in external mode for Python IDE

  • 1.  How to install/import SpssClient in external mode for Python IDE

    Posted Fri March 18, 2022 02:24 PM
    I need to call SPSS Logistic Regression within external mode in a Python IDE. I currently have SPSS 27 pointed to Python 3.8 installed in a virtual environment in Anaconda. The first step in the IDE of course is to import spss, SpssClient, and spssaux probably, but I'm not sure how to get these from SPSS into the virtual environment I'm launching my IDE from. Once the basic SPSS packages are loaded in the IDE and a session in SpssClient is opened, what methods would be used to run a Logistic Regression on data set up in the IDE in, say, pandas?

    ------------------------------
    Kristian Canler
    ------------------------------

    #SPSSStatistics


  • 2.  RE: How to install/import SpssClient in external mode for Python IDE

    Posted Fri March 18, 2022 02:44 PM
    Edited by System Admin Fri January 20, 2023 04:43 PM
    At this point I've copy pasted the site-packages folder from C:\Program Files\IBM\SPSS\Statistics\27\Python3\Lib (working in Windows 10) to C:\ProgramData\Anaconda3\envs\spss\Lib, but the spss package still won't import.

    I've also tried redirecting the Python path to SPSS like this

    import sys
    
    sys.path.insert(0, r'C:\Program Files\IBM\SPSS\Statistics\27\Python3\Lib\site-packages')

    And got the following error when I tried to import spss:

    ImportError: Module use of python38.dll conflicts with this version of Python.

    This is even though the version of Python is 3.8.12.

    #SPSSStatistics


  • 3.  RE: How to install/import SpssClient in external mode for Python IDE

    Posted Fri March 18, 2022 02:52 PM
    I have never tried to do this; I've only run modules from with SPSS itself and python (without SPSS). But from what I see here, for example, it looks like for any module to be successfully called, sys.path must be correctly defined:

    Python looks for modules in "sys.path"

    Python has a simple algorithm for finding a module with a given name, such as a_module. It looks for a file called a_module.py in the directories listed in the variable sys.path.



    ------------------------------
    Rick Marcantonio
    Quality Assurance
    IBM
    ------------------------------



  • 4.  RE: How to install/import SpssClient in external mode for Python IDE

    Posted Fri March 18, 2022 03:06 PM
    I use an external Python IDE (Wing) all the time.  In order to enable external mode, I need to set the Python executable in Wing preferences, including location to the one that SPSS uses, which would normally be the one that is installed with SPSS, but if you are using the one installed with Anaconda and specified in Edit > Options > Files, it would be different.

    You can find the location by running this in a syntax window.
    begin program python3.
    import sys
    print(sys.executable)
    end program.

    If you are using a different Python from the Statistics installed version, you might need to create a sitecustomize.py file in the lib\site-packages folder under that that appends the Statistics site-packages folder to sys.path.  But also look at the pythonenv.bat file or equivalent that is in the Statistics installation folder.

    I haven't used Anaconda for a few releases, but that might add some extra quirks.


    --





  • 5.  RE: How to install/import SpssClient in external mode for Python IDE

    Posted Fri March 18, 2022 03:24 PM
    Hi Jon & Rick; thanks for your answers.

    Setting the Python executable to the location the SPSS uses is fine as long as it's possible to install packages like pandas there. Is this doable?

    ------------------------------
    Kristian Canler
    ------------------------------



  • 6.  RE: How to install/import SpssClient in external mode for Python IDE

    Posted Fri March 18, 2022 03:36 PM
    Yes.  I suggest that you use the STATS PACKAGE INSTALL extension command (Extensions > Install Python and R modules) to do this, although that might not be necessary.  You might need to start Statistics in Admin mode to do it depending on your system configuration and where you installed Statistics.  You can install that package via the Extensions > Extension Hub menu.  For Python modules, PACKAGE INSTALL will look on PyPI for packages.

    On the other part of your first post, when you want to run logistic regression or other SPSS commands from Python, you would use the spss.Submit api.  That accepts one or more SPSS commands.

    --





  • 7.  RE: How to install/import SpssClient in external mode for Python IDE

    Posted Fri March 18, 2022 04:48 PM
    Edited by System Admin Fri January 20, 2023 04:33 PM
    Thanks for the direction to spss.Submit.

    STATS PACKAGE INSTALL worked great for installing Python packages, but my IDE is still having trouble importing spss with an error message saying that PyInvokeSpss, couldn't be found:

    ---------------------------------------------------------------------------
    ImportError                               Traceback (most recent call last)
    ~\AppData\Local\Temp/ipykernel_6052/2942303256.py in <module>
    ----> 1 import spssaux
    
    C:\Program Files\IBM\SPSS\Statistics\27\Python3\Lib\site-packages\spssaux\__init__.py in <module>
         10 # ************************************************************************/
         11 
    ---> 12 from .spssaux import *
         13 from .spssaux import __version__, _buildvarlist, _smartquote, _isseq, _listify
    
    C:\Program Files\IBM\SPSS\Statistics\27\Python3\Lib\site-packages\spssaux\spssaux.py in <module>
         14 # ************************************************************************/
         15 
    ---> 16 import spss
         17 import random
         18 import copy, sys, os.path, os
    
    C:\Program Files\IBM\SPSS\Statistics\27\Python3\Lib\site-packages\spss\__init__.py in <module>
        254 __SetErrorMessage()
        255 
    --> 256 from .spss import *
        257 from .cursors import *
        258 from .pivotTable import *
    
    C:\Program Files\IBM\SPSS\Statistics\27\Python3\Lib\site-packages\spss\spss.py in <module>
         21 
         22 import atexit,os,locale,sys,codecs
    ---> 23 from . import PyInvokeSpss
         24 from .errMsg import *
         25 
    
    ImportError: DLL load failed while importing PyInvokeSpss: The specified module could not be found.
    When I tried to install PyInvokeSpss with the install modules extension, I got this error in the output window:

    C:\PROGRA~1\IBM\SPSS\STATIS~1\27\Python3\Lib\site-packages\pip\_vendor\ipaddress.py:1106: SyntaxWarning: 'str' object is not callable; perhaps you missed a comma?
    raise TypeError("%s and %s are not of the same version" (a, b))
    WARNING: pip is configured with locations that require TLS/SSL, however the ssl module in Python is not available.
    Collecting PyInvokeSpss
    WARNING: Retrying (Retry(total=4, connect=None, read=None, redirect=None, status=None)) after connection broken by 'SSLError("Can't connect to HTTPS URL because the SSL module is not available.")': /simple/pyinvokespss/
    WARNING: Retrying (Retry(total=3, connect=None, read=None, redirect=None, status=None)) after connection broken by 'SSLError("Can't connect to HTTPS URL because the SSL module is not available.")': /simple/pyinvokespss/
    WARNING: Retrying (Retry(total=2, connect=None, read=None, redirect=None, status=None)) after connection broken by 'SSLError("Can't connect to HTTPS URL because the SSL module is not available.")': /simple/pyinvokespss/
    WARNING: Retrying (Retry(total=1, connect=None, read=None, redirect=None, status=None)) after connection broken by 'SSLError("Can't connect to HTTPS URL because the SSL module is not available.")': /simple/pyinvokespss/
    WARNING: Retrying (Retry(total=0, connect=None, read=None, redirect=None, status=None)) after connection broken by 'SSLError("Can't connect to HTTPS URL because the SSL module is not available.")': /simple/pyinvokespss/
    Could not fetch URL https:pypi.org/simple/pyinvokespss/: There was a problem confirming the ssl certificate: HTTPSConnectionPool(host='pypi.org', port=443): Max retries exceeded with url: /simple/pyinvokespss/ (Caused by SSLError("Can't connect to HTTPS URL because the SSL module is not available.")) - skipping
    ERROR: Could not find a version that satisfies the requirement PyInvokeSpss (from versions: none)
    ERROR: No matching distribution found for PyInvokeSpss


    ------------------------------
    Kristian Canler
    ------------------------------



  • 8.  RE: How to install/import SpssClient in external mode for Python IDE

    Posted Fri March 18, 2022 05:20 PM
    PyInvokeSpss is part of the standard Statistics Python3 installation.  It lives in the site-packages\spss directory.  It's actually a pyd file, and PACKAGE INSTALL would never find it as it isn't on PyPI.  If Python can find the spss module, it should have no trouble finding that module.  You would never import it directly.  The spss module depends heavily on it.

    Can you successfully import the spss module in internal mode and external mode?  Try that by itself.

    These warnings are strange
    C:\PROGRA~1\IBM\SPSS\STATIS~1\27\Python3\Lib\site-packages\pip\_vendor\ipaddress.py:1106: SyntaxWarning: 'str' object is not callable; perhaps you missed a comma?
    raise TypeError("%s and %s are not of the same version" (a, b))
    WARNING: pip is configured with locations that require TLS/SSL, however the ssl module in Python is not available.
    They make me wonder whether there is something unusual about your installation, but let's get an answer to my question above.

    --





  • 9.  RE: How to install/import SpssClient in external mode for Python IDE

    Posted Sat March 19, 2022 08:59 PM
    I can successfully import spss in the SPSS window with

    begin program Python3.
    import spss
    end program.

    In my IDE, however, when I try to import spss I get the error about not being about to find PyInvokeSpss: "ImportError: DLL load failed while importing PyInvokeSpss: The specified module could not be found." I think this is because Anaconda is running on a Python executable in the Anaconda directory, but I can't find a way of redirecting Anaconda to the Python executable in the SPSS 27 directory, if that's what's needed. I'm not really particular on what solution to use, I'm just trying to successfully import spss in an Anaconda environment (or any IDE really).





  • 10.  RE: How to install/import SpssClient in external mode for Python IDE

    Posted Sun March 20, 2022 01:00 PM
    It is strange that the spss.py file can be found but not PyInvoke.pyd, which is in the same folder.
    Check what is being used externally by running
    import sys
    print(sys.executable)

    Compare that with what you see when you run that code as a program within Statistics.

    But PyInvokeSpss should not be imported directly.  The spss module should handle that automatically.  You should be able to code like this
    import spss
    spss.PyInvokeSpss.IsUTF8mode()

    What are you using for your IDE?  If it is IDLE, which is part of the standard Python distribution, it doesn't have a way to configure the Python executable being used AFAIK.

    But you should be able to do something like this to get external mode working.  Try this before importing spss in a session
    import sys
    sys.path.insert(0, r"the location shown for the executable in internal mode/lib/site-packages")

    If that works, you can put that in a sitecustomize.py file in the site-packages directory under the sys.executable folder shown in external mode.  That way, it would look in the proper site-packages directory even though you started a different python executable.

    If you are using IDLE - I don't know whether Anaconda now includes an IDE, I suggest trying out the free version of Wing, which is Wing Personal at Download Wing Personal v. 8.2.0 - Wing Python IDE or the free version of pycharm.  I use the Professional version of Wing, which is not free but is inexpensive.  It has many more features, but the free version might be enough for your needs.



    ------------------------------
    Jon Peck
    ------------------------------



  • 11.  RE: How to install/import SpssClient in external mode for Python IDE

    Posted Mon March 21, 2022 09:55 AM
    import sys
    sys.path.insert(0, r"the location shown for the executable in internal mode/lib/site-packages")

    I think this is what I described attempting to do in post #2; is that right?

    When I print sys.executable internally in SPSS, I get this path:

    C:\PROGRA~1\IBM\SPSS\STATIS~1\27\startx.exe

    When I print sys.executable in my IDE (JupyterLab), even after running sys.path.insert(0, r'C:\Program Files\IBM\SPSS\Statistics\27\Python3\Lib\site-packages'), I get the following path to the Anaconda Python executable (and not even the executable in the directory for the virtual environment I launched JupyterLab from): C:\ProgramData\Anaconda3\python.exe

    I've been looking into ways to set the executable in JupyterLab (or anywhere) to the executable in SPSS, but haven't succeeded in doing that.




    ------------------------------
    Kristian Canler
    ------------------------------



  • 12.  RE: How to install/import SpssClient in external mode for Python IDE

    Posted Mon March 21, 2022 11:55 AM
    I suspect that JupyterLab has already started Python when you get control, so it is too late to change the executable using the path.

    I found this video that shows how to change the Python version JupyterLab uses

    Since the IDE seems to be finding the spss.py module but reporting not loading PyInvokeSpss, I suspect that the error is not actually that it can't find the file, which is in that same directory.  PyInvokeSpss is actually a (compiled) dll.  So if Anaconda has compiled the Python executable with a different compiler from the one used to compile PyInvokeSpss, there could be a conflict in the runtimes that each requires.  So it would be finding PyInvokeSpss but be unable to load it.

    If you give it the Python installation Statistics uses, which is not set as the system standard, it ought to work.

    I will also query the dev team to see if they have any experience with this.  Let me know what you find out.

    --





  • 13.  RE: How to install/import SpssClient in external mode for Python IDE

    Posted Mon March 21, 2022 02:22 PM
    Edited by System Admin Fri January 20, 2023 04:41 PM
    Thanks again for your troubleshooting help here. Using the %%script iPython magic suggested in the video tutorial seems to do the trick, but it only works within the same cell.

    %%script C:\Program Files\IBM\SPSS\Statistics\27\Python3\python.exe

    import sys

    print(sys.executable)

    > C:\Program Files\IBM\SPSS\Statistics\27\Python3\python.exe
    It's also possible to successfully import and call SpssClient with that magic:

    %%script C:\Program Files\IBM\SPSS\Statistics\27\Python3\python.exe

    import SpssClient

    SpssClient.StartClient()

    SpssClient.StopClient()

    However, because of the way %%script works, it's not possible to break these Python statements into multiple cells, even with the %%script magic at the top of each cell. The end of the cell terminates/executes the cell content like a script, and the next cell "resets," in the sense that SpssClient was imported only to that cell, not to the notebook as a whole. This would suggest (a) using a different IDE or (b) finding a different solution in Jupyter, for example, finding a way to launch Jupyter based on an executable outside Anaconda.

    It's worth noting it doesn't seem possible to launch Jupyter from the SPSS directory in Windows because it's in C:\ProgramData.



    ------------------------------
    Kristian Canler
    ------------------------------



  • 14.  RE: How to install/import SpssClient in external mode for Python IDE

    Posted Mon March 21, 2022 03:15 PM
    Progress!  Maybe there is a way to set a cross-cell parameter.  But the bigger problem if each cell in JL is independent, then the spss connection and state are probably not maintained from one cell to the next.  That would pretty much limit you to a single cell environment (call it amoeba).

    --





  • 15.  RE: How to install/import SpssClient in external mode for Python IDE

    Posted Mon March 21, 2022 04:10 PM
    That's right -- it would mean that SPSS can't be run externally in Jupyter (but I would find that surprising since Jupyter is the most common IDE for data-oriented Python work). Let me know what you get back from the dev team, and thanks again.

    ------------------------------
    Kristian Canler
    ------------------------------



  • 16.  RE: How to install/import SpssClient in external mode for Python IDE

    Posted Mon March 21, 2022 04:29 PM
    This stackexchange note might help.  There really ought to be a way to maintain state between cells, but, even better, there might be a way to run a startup script so that you don't have to run that initialization cell.


    And there is

    --