4

Inspired by this question and answer I wrote a simple "set current working directory". But sometimes it throws this error:

>Warning # 6894.  Command name: BEGIN PROGRAM 
>The external program exit unexpectedly and lost its content, a new exteranl 
>program will startup to execute the rest of job.

I cannot lead this error to anything special in my code, SPSS throws this error sometimes, if I keep executing the program sometimes it works and sometimes it does not with above error, so it feels like if the client sometimes is not started or something. My program looks like this:

def Run(args):
    import spss, spssaux, SpssClient, os
    SpssClient.StartClient()

    my_filepath_ = os.path.dirname(SpssClient.GetDesignatedSyntaxDoc().GetDocumentPath())
    my_filepath = spssaux._smartquote(my_filepath_)
    spss.Submit("CD %s ." % (my_filepath))
    SpssClient.StopClient()

I have also set up a XML file and placed it along with the python file in the EXTPATHS EXTENSIONS directory.

<Command xmlns="http://xml.spss.com/extension" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" Name="SET_CWD" Language="Python">
</Command>

It is called by typing SET_CWD in a syntax file, SET_CWD is also the name of the .py- and .xml-file, SET_CWD.py and SET_CWD.xml.

Cœur
  • 37,241
  • 25
  • 195
  • 267
honetkri
  • 67
  • 5
  • What file name have you saved this file with this python function? Have you setup a corresponding xml file also to be able to run the code as an extension command? Otherwise how are you calling the code? – Jignesh Sutar Jan 09 '17 at 10:30
  • I have updated the question with more information! – honetkri Jan 09 '17 at 10:37
  • A couple of changes required. Remove the underscore in the xml file with code "SET_CWD" and also when calling in the syntax file. I have just setup these files and the code as such and it works just fine. My choice of using the word "SET" was probably a bad example as it clashes with the native SPSS command and so should be avoided. – Jignesh Sutar Jan 09 '17 at 11:17
  • I also saved both files in the folder: "C:\ProgramData\IBM\SPSS\Statistics\24\extensions" – Jignesh Sutar Jan 09 '17 at 11:29
  • hm, how weird, I changed the name `SET_CWD` to `SETCWD` in all files instead (due to the `SET` command in SPSS). But it still throws error. What if you try to execute the program, then use `CD` to change into another directory and then execute the program again, still no error? because this is what is causing the error for me. It works the first time but when using `CD` and then the program again it does not work. – honetkri Jan 09 '17 at 11:47

2 Answers2

1

I have these two files saved in the folder C:\ProgramData\IBM\SPSS\Statistics\24\extensions

MYSET_CWD.py

def Run(args):
    import spss, spssaux, SpssClient, os
    SpssClient.StartClient()

    my_filepath_ = os.path.dirname(SpssClient.GetDesignatedSyntaxDoc().GetDocumentPath())
    my_filepath = spssaux._smartquote(my_filepath_)
    spss.Submit("CD %s ." % (my_filepath))
    SpssClient.StopClient()

MYSET_CWD.xml

<Command xmlns="http://xml.spss.com/extension" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" Name="MYSET CWD" Language="Python">
</Command>

In a saved syntax, it must be saved (else you will get an error message No valid directory specification was found.), I run:

MYSET CWD.

And the current working directory is changed to the saved location of the syntax file from which MYSET CWD. was run from. This is confirmed by running SHOW directory. before and after MYSET CWD..

Jignesh Sutar
  • 2,909
  • 10
  • 13
  • Se my comment above, i am still getting the same error after executing a `CD` command and then `MYSET CWD`, maybe it is something with the python extension. – honetkri Jan 09 '17 at 12:25
  • `MYSET CWD.` `>Warning # 6894. Command name: BEGIN PROGRAM` `>The external program exit unexpectedly and lost its content, a new exteranl` `>program will startup to execute the rest of job.` Try executing this and I always get above error: `SHOW DIRECTORY.` `MYSET CWD.` `SHOW DIRECTORY.` `CD '/Users/user'.` `SHOW DIRECTORY.` `MYSET CWD.` `SHOW DIRECTORY.` – honetkri Jan 09 '17 at 12:36
  • weird! must be something with my python extension then... I'll accept this answer anyhow. – honetkri Jan 09 '17 at 13:06
  • Try running the python def function in interactive SPSS syntax wrapped around `BEGIN PROGRAM / END PROGRAM`. Does that cause any errors? Always restart your SPSS session when editing or create new .py / .xml files. – Jignesh Sutar Jan 09 '17 at 13:08
  • Tried another computer with SPSS 23, currently on SPSS 20, and it is working fine on that computer, so either SPSS version or something with python extension. – honetkri Jan 10 '17 at 07:20
0

Note that GetDocumentPath "Returns the path and file name of the syntax file associated with this syntax document object, or the empty string if the syntax document is not associated with a file."

So you should test that condition before trying to do anything else.

The XML file has nothing to do with this. If it is incorrect, you will get an error when you try to run your extension command before your code gets control.

In some situations, you might not have a designated syntax window (or it might not be saved).

JKP
  • 5,419
  • 13
  • 5