SPSS Statistics

 View Only
  • 1.  Reload a Python extension without restarting SPSS?

    Posted Mon January 04, 2021 10:12 AM
    Hi,
    I'm working on a SPSS extension written in Python3. I have the MYCOMMAND.xml and MYCOMMAND.py files and in order to reflect the changes I did in my Python code, I need to restart SPSS. 

    I tried the EXTENSION command as follows but that does not seem to work:

    EXTENSION ACTION=REMOVE
    /SPECIFICATION COMMAND='/Users/jan/Documents/SPSS/Extensions/MYCOMMAND.xml'.

    EXTENSION
    /SPECIFICATION COMMAND='/Users/jan/Documents/SPSS/Extensions/MYCOMMAND.xml'.

    MYCOMMAND .

    The Python code executed is still the old one and only restarting SPSS helps.

    Isn't there a way to reload a given extension without restarting SPSS?

    cheers,
    Jan

    ------------------------------
    Jan C
    ------------------------------

    #SPSSStatistics


  • 2.  RE: Reload a Python extension without restarting SPSS?
    Best Answer

    IBM Champion
    Posted Mon January 04, 2021 10:19 AM
    When Python imports a module, if the module has already been imported, it does not reimport it.  You can force a reimport like this.
    begin program python3.
    import importlib
    importlib.reload(module-name)
    end program.

    This does not always work, but if it doesn't you need to restart Statistics.

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



  • 3.  RE: Reload a Python extension without restarting SPSS?

    Posted Mon January 04, 2021 11:51 AM
    Hi,
    nice, that works and does indeed reload my Python extension. Thanks!

    While you are here .. can you confirm that EXTENSION command can load the .xml from any path but the actual implementation (.py) has to be in any of the SPSS paths (output of SHOW EXTPATHS.

    cheers


    ------------------------------
    Jan C
    ------------------------------



  • 4.  RE: Reload a Python extension without restarting SPSS?

    IBM Champion
    Posted Mon January 04, 2021 12:14 PM
    The EXTENSION command could load the xml file from anywhere, but if the extension is to be automatically available in subsequent sessions, the file needs to be on the EXTENSIONS path.

    Similarly, the Python or R extension code needs to be on the path.  In case you are not aware of it, there are two environment variables you can set to extend the search paths.
    Extend the paths for custom dialogs ...
    SPSS_CDIALOGS_PATH=C:\dlgs3;C:\dlgs
    Extend the path for Python or R code... 
    SPSS_EXTENSIONS_PATH=c:\extcommon3;
    These are placed at the start of the search paths.  I use these so that I don't have to reinstall extensions with each new version of Statistics, but they are helpful in dealing with situations where Admin privileges would be required otherwise.  

    If you have both Python2 and Python3 code, you can prioritize the search accordingly.

    This variable is helpful for developers of extensions.  It causes the traceback in an extension command to be displayed when an exception occurs.  By default, the tracebacks are suppressed, because they would be meaningless to users....
    SPSS_EXTENSIONS_RAISE=true

    --





  • 5.  RE: Reload a Python extension without restarting SPSS?

    Posted Mon January 04, 2021 12:19 PM
    Understood, thanks

    ------------------------------
    Jan
    ------------------------------