python-3.xstatisticsspss

How to properly invoke Python 3 script from SPSS syntax window using SCRIPT command (+ additional problems during runtime)


I would like to run two Python 3 scripts from SPSS syntax window. It is possible to perform it using BEGIN PROGRAM-END PROGRAM. block or SCRIPT command. This time I need to find a solution using second command.

Simplified code:

*** MACROS.
define export_tabs (!positional !tokens (1))

  output modify
  /select logs headings texts warnings pagetitles outlineheaders notes
  /deleteobject delete = yes.

  OUTPUT EXPORT
  /CONTENTS EXPORT = visible  LAYERS = printsetting  MODELVIEWS = printsetting
  /XLSX  DOCUMENTFILE = "doc.xlsx"
  OPERATION = createsheet
  sheet = !quote(!unquote(!1))
  LOCATION = lastcolumn NOTESCAPTIONS = no

!enddefine.

define matrix_tab (!positional !charend('/')
  /!positional !charend('/')
  /!positional !charend('/')
  /!positional !charend('/')
  /stat = !tokens (1))

  !do !i !in (!3)

  ctables
  /mrsets countduplicates = no
  /vlabels variables = !concat(!1,_,!2,_,!i) display = label
  /table !concat(!1,_,!2,_,!i)
  [rowpct.responses.count !concat(!unquote(!stat),"40.0"), totals[count f40.0]]
  /slabels position = column visible = no
  /clabels rowlabels = opposite
  /categories variables = !concat(!1,_,!2,_,!i) order = a key = value
  empty = include total = yes label = "VALID COUNT" position = after
  /titles title = !upcase(!4).

  !doend

!enddefine.

*** REPORT.
* Sheet 1.
output close all.
matrix_tab $Q1 / 1 / 1 2 / "QUESTION 1" / stat="pct".

script "C:\path\script 1.py".
script "C:\path\script 2.py".

export_tabs "Q1".

* Sheet 2.
output close all.
matrix_tab $Q2 / 2 / 3 4 / "QUESTION 2" / stat="pct".

script "C:\path\script 1.py".
script "C:\path\script 2.py".

export_tabs "Q2".

When I run a block for the first sheet everything works fine. However, when I run a block for the second sheet SPSS doesn't execute Python scripts and jumps straight to export_tabs macro (problems with synchronization?). I thought a problem had been possibly in a way I executed SCRIPT command. So I tried this:

script "C:\path\script 1.py" pythonversion = 3.
script "C:\path\script 2.py" pythonversion = 3.

but in effect SPSS - even though the syntax window coloured these parts of syntax - returned this error message:

>Error # 3251 in column 152.  Text: pythonversion 
>The SCRIPT command contains unrecognized text following the the file 
>specification.  The optional parameter must be a quoted string enclosed in 
>parentheses. 
>Execution of this command stops.

Has anyone of you had such problem and/or have an idea why this happens?

NOTE: Both Python scripts run smoothly from the Python 3.4.3 shell installed with the version of SPSS I have, thus I don't think the core of the problem is within those codes.


Solution

  • This seems to be a document defect in the way this keyword was implemented. I have been able to replicate it and have logged a defect with IBM SPSS Statistics Development.

    In this case, the order matters. Rather than this:

    script "C:\path\script 2.py" pythonversion = 3.
    

    Try instead:

    script pythonversion = 3 "C:\path\script 2.py".