Python extension command / reload Python code

classic Classic list List threaded Threaded
3 messages Options
Reply | Threaded
Open this post in threaded view
|

Python extension command / reload Python code

Joachim Wackerow-2

Hello,

 

I would like to make additions to an existing extension command which is using a Python script. I noticed that any change to the Python code is ignored after the command is already executed in SPSS (even if the compiled Python file is deleted).

 

Now I’m wondering how it would be possible to test any changes to the Python code without restarting SPSS each time.

 

Any hint is appreciated.

 

Cheers,

Achim

Reply | Threaded
Open this post in threaded view
|

Re: Python extension command / reload Python code

Jon K Peck
Python code is loaded by the Python import module-name statement.  It is smart enough not to repeat the work if the module has already been loaded in the current session.  Restarting Statistics guarantees the reload.  Deleting the .pyc file doesn't help, because the code is already loaded.

But there is a solution.  Use the reload function to force the module to be reloaded.  So you could write
begin program.
import foo
reload(foo)
...
end program.

Now, if this is an extension command, you need to be a little bit more roundabout.  I typically keep a little program block like
begin program.
import foo
reload(foo)
end program.
sitting in a syntax window and rerun it to force the reload.

The one caveat here is that reloading does not recreate any objects left over from a previous run.  Since program objects persist between programs within a session, they could be stale.  However, in most cases an extension command would not be reusing previous objects, so this is rarely a problem.

Also, you might consider developing in external mode.  I typically develop and run code from my IDE, so each run is actually a new Statistics session.  This is easier to debug, and the startup time to run it is less than if I have to restart Statistics.  Of course, if you are working with Viewer objects or other parts of the UI, that strategy does not work.

HTH,
Jon Peck (no "h") aka Kim
Senior Software Engineer, IBM
[hidden email]
new phone: 720-342-5621




From:        "Wackerow, Joachim" <[hidden email]>
To:        [hidden email]
Date:        03/09/2012 06:26 AM
Subject:        [SPSSX-L] Python extension command / reload Python code
Sent by:        "SPSSX(r) Discussion" <[hidden email]>




Hello,
 
I would like to make additions to an existing extension command which is using a Python script. I noticed that any change to the Python code is ignored after the command is already executed in SPSS (even if the compiled Python file is deleted).
 
Now I’m wondering how it would be possible to test any changes to the Python code without restarting SPSS each time.
 
Any hint is appreciated.
 
Cheers,
Achim
Reply | Threaded
Open this post in threaded view
|

Re: Python extension command / reload Python code

Joachim Wackerow-2

Jon,

 

Thanks a lot for the quick answer. I just tried it. It works very well with the import and reload.

Thank you for the explanation. This is helpful.

 

Cheers,

Achim

 

--

GESIS - Leibniz Institute for the Social Sciences

Department: Monitoring Society and Social Change

Unit: Social Science Metadata Standards

Visiting address: B2 1, 68159 Mannheim, Germany

Postal address: P.O. Box 122155, 68072 Mannheim, Germany

Phone: +49 (0)621 1246 262

Fax: +49 (0)621 1246 100

E-mail: [hidden email]

www.gesis.org/en/institute/

 

From: Jon K Peck [mailto:[hidden email]]
Sent: Freitag, 9. März 2012 15:01
To: Wackerow, Joachim
Cc: [hidden email]
Subject: Re: [SPSSX-L] Python extension command / reload Python code

 

Python code is loaded by the Python import module-name statement.  It is smart enough not to repeat the work if the module has already been loaded in the current session.  Restarting Statistics guarantees the reload.  Deleting the .pyc file doesn't help, because the code is already loaded.

But there is a solution.  Use the reload function to force the module to be reloaded.  So you could write
begin program.
import foo
reload(foo)
...
end program.

Now, if this is an extension command, you need to be a little bit more roundabout.  I typically keep a little program block like
begin program.
import foo
reload(foo)
end program.
sitting in a syntax window and rerun it to force the reload.

The one caveat here is that reloading does not recreate any objects left over from a previous run.  Since program objects persist between programs within a session, they could be stale.  However, in most cases an extension command would not be reusing previous objects, so this is rarely a problem.

Also, you might consider developing in external mode.  I typically develop and run code from my IDE, so each run is actually a new Statistics session.  This is easier to debug, and the startup time to run it is less than if I have to restart Statistics.  Of course, if you are working with Viewer objects or other parts of the UI, that strategy does not work.

HTH,
Jon Peck (no "h") aka Kim
Senior Software Engineer, IBM
[hidden email]
new phone: 720-342-5621




From:        "Wackerow, Joachim" <[hidden email]>
To:        [hidden email]
Date:        03/09/2012 06:26 AM
Subject:        [SPSSX-L] Python extension command / reload Python code
Sent by:        "SPSSX(r) Discussion" <[hidden email]>





Hello,
 
I would like to make additions to an existing extension command which is using a Python script. I noticed that any change to the Python code is ignored after the command is already executed in SPSS (even if the compiled Python file is deleted).
 
Now I’m wondering how it would be possible to test any changes to the Python code without restarting SPSS each time.
 
Any hint is appreciated.
 
Cheers,
Achim