SPSS Statistics

SPSS Statistics

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

 View Only
  • 1.  running a python script within spss

    Posted Tue April 01, 2025 09:08 AM

    I have an anaconda python script i'd like to start running but call it from within SPSS.  I've attempted to use HOST, & a python subprocess block but am getting an error.  My guess is that it's trying to use the python3 within SPSS.  I can get the subprocess block to run outside of it.  - Thanks Art  



    ------------------------------
    Art Jack
    ------------------------------


  • 2.  RE: running a python script within spss

    Posted Tue April 01, 2025 09:29 AM
    Within SPSS, the Python invoked would be the one set up by the virtual environment, but it still ought to run unless it uses something incompatible with Python 3.10.  If it depends on some module not on the Python path, you would need to augment PYTHONPATH before invoking HOST.

    What is the error you are getting?


    --





  • 3.  RE: running a python script within spss

    Posted Tue April 01, 2025 03:26 PM
    Edited by Art Jack Tue April 01, 2025 03:31 PM

    when trying to run a subprocess block i'm getting this, i'm reading this as i have conflicting libraries which makes sense: 


    Error: Traceback (most recent call last):
      File "C:\General Syntax 24-25\alc.py", line 9, in <module>
        import glob
      File "C:\PROGRA~1\IBM\SPSSST~1\Python3\Lib\glob.py", line 5, in <module>
        import re
      File "C:\PROGRA~1\IBM\SPSSST~1\Python3\Lib\re.py", line 125, in <module>
        import sre_compile
      File "C:\PROGRA~1\IBM\SPSSST~1\Python3\Lib\sre_compile.py", line 17, in <module>
        assert _sre.MAGIC == MAGIC, "SRE module mismatch"
               ^^^^^^^^^^^^^^^^^^^
    AssertionError: SRE module mismatch

    ERROR : (1) No error.


    Traceback (most recent call last):
      File "C:\General Syntax 24-25\alc.py", line 12, in <module>
        import pandas as pd
    ModuleNotFoundError: No module named 'pandas'

    I've tried a few different host command ways.  One calling the .PY file directly & another try to run a .bat file.  I haven't found much documentation on how to use it, & am probably missing a step.  I'm getting exit status 001, and a unicode error other times.



    ------------------------------
    Art Jack
    ------------------------------



  • 4.  RE: running a python script within spss

    Posted Tue April 01, 2025 06:38 PM
    That does look like a Python version mismatch.  What do you see if you run
    import sys
    print(sys.__version__) 
    from within SPSS and via standalone?

    You would need to modify the PYTHONPATH setting to get the right Python version or maybe do a SET command to point to the SPSS version in the bat file before invoking Python.


    --





  • 5.  RE: running a python script within spss

    Posted Wed April 02, 2025 11:01 AM

    I'm a little hesitant to change any python configuration because i'm also using the spss installed python for lots of other things.  I'll probably just set up another process outside through subprocess. 

    Is there a way to link batch jobs with dependencies?  I've asked a similar question before when i came across the luigi python module.  I haven't been able to implement it though.  I have many dependent SPSS & Python scripts running but if one fails (or runs out of turn) it can get complicated. 



    ------------------------------
    Art Jack
    ------------------------------



  • 6.  RE: running a python script within spss

    Posted Wed April 02, 2025 06:00 PM
    Look at the Windows Task Scheduler. That might help to control jobs.  It can respond to certain kinds of events.  Or write a Python controller to manage them.

    As for Python versions, you would set the PYTHONPATH as part of the HOST file code if you need to specify the SPSS-installed version if there is another Python version that is set up to run from a command file,\.


    --