Error while creating user-defined functions with Python

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

Error while creating user-defined functions with Python

Luca Meyer
Hello SPSSX-Listers,

I am trying to test the Python code to reduce string length (as
described on page 286 of Raynald's data management book).

I have copied the Python code provided in the book and saved it in
C:\Python24\Lib\site-packages\shortenstring.py (a copy of this file is
downloadable from http://www.lucameyer.com/shortenstring.py)

I have then tryed to run the following syntax (the one given in the book
slightly modified).

///

*python_reformat_string.sps.
DATA LIST FREE /string1 (A10) string2 (A10) string3 (A10).
BEGIN DATA
a ab abc
a abcde ab
abcdef abcdefgh abcdefghi
END DATA.

BEGIN PROGRAM.
import shortenstring
spss.Submit("SET PRINTBACK=YES MPRINT=YES.")
shortenstring.ReformatString(['string1','string2','string3'])
END PROGRAM.

///

And I get:

///

*python_reformat_string.sps.
503 M>  *python_reformat_string.sps.
DATA LIST FREE /string1 (A10) string2 (A10) string3 (A10).
504 M>  DATA LIST FREE /string1 (A10) string2 (A10) string3 (A10).
BEGIN DATA
505 M>  BEGIN DATA
a ab abc
a abcde ab
abcdef abcdefgh abcdefghi
END DATA.

509 M>
BEGIN PROGRAM.
510 M>  BEGIN PROGRAM.
import shortenstring
spss.Submit("SET PRINTBACK=YES MPRINT=YES.")
shortenstring.ReformatString(['string1','string2','string3'])
END PROGRAM.
514 M>  SET PRINTBACK=YES MPRINT=YES.
515 M>  BEGIN PROGRAM '#
'.
Traceback (most recent call last):
  File "<string>", line 3, in ?
  File "C:\Python24\lib\site-packages\shortenstring.py", line 9, in
ReformatString
    import spss, random
NameError: global name 'spss' is not defined

///

As you will noticed I have enclosed in the shortentring.py the line:

import spss, random

as I was adviced by Raynald to do but still I get
errors...

I am using SPSS 15.

Thank you in advance for your assistance,

Luca

No virus found in this outgoing message.
Checked by AVG Free Edition.
Version: 7.5.488 / Virus Database: 269.13.39/1045 - Release Date: 02/10/2007
18.43
Reply | Threaded
Open this post in threaded view
|

Re: Error while creating user-defined functions with Python

Peck, Jon
You must import each module you use in your Python code at the same level where you reference it.

begin program.
import spss, shortenstring
...

Importing spss inside the shortenstring module does not make it available to the upper code.  Internally, Python will only do the import once, but the visibility of the contents is governed by where the import statements appear.

Regards,
Jon Peck


-----Original Message-----
From: SPSSX(r) Discussion [mailto:[hidden email]] On Behalf Of Luca Meyer
Sent: Wednesday, October 03, 2007 4:02 AM
To: [hidden email]
Subject: [SPSSX-L] Error while creating user-defined functions with Python

Hello SPSSX-Listers,

I am trying to test the Python code to reduce string length (as
described on page 286 of Raynald's data management book).

I have copied the Python code provided in the book and saved it in
C:\Python24\Lib\site-packages\shortenstring.py (a copy of this file is
downloadable from http://www.lucameyer.com/shortenstring.py)

I have then tryed to run the following syntax (the one given in the book
slightly modified).

///

*python_reformat_string.sps.
DATA LIST FREE /string1 (A10) string2 (A10) string3 (A10).
BEGIN DATA
a ab abc
a abcde ab
abcdef abcdefgh abcdefghi
END DATA.

BEGIN PROGRAM.
import shortenstring
spss.Submit("SET PRINTBACK=YES MPRINT=YES.")
shortenstring.ReformatString(['string1','string2','string3'])
END PROGRAM.

///

And I get:

///

*python_reformat_string.sps.
503 M>  *python_reformat_string.sps.
DATA LIST FREE /string1 (A10) string2 (A10) string3 (A10).
504 M>  DATA LIST FREE /string1 (A10) string2 (A10) string3 (A10).
BEGIN DATA
505 M>  BEGIN DATA
a ab abc
a abcde ab
abcdef abcdefgh abcdefghi
END DATA.

509 M>
BEGIN PROGRAM.
510 M>  BEGIN PROGRAM.
import shortenstring
spss.Submit("SET PRINTBACK=YES MPRINT=YES.")
shortenstring.ReformatString(['string1','string2','string3'])
END PROGRAM.
514 M>  SET PRINTBACK=YES MPRINT=YES.
515 M>  BEGIN PROGRAM '#
'.
Traceback (most recent call last):
  File "<string>", line 3, in ?
  File "C:\Python24\lib\site-packages\shortenstring.py", line 9, in
ReformatString
    import spss, random
NameError: global name 'spss' is not defined

///

As you will noticed I have enclosed in the shortentring.py the line:

import spss, random

as I was adviced by Raynald to do but still I get
errors...

I am using SPSS 15.

Thank you in advance for your assistance,

Luca

No virus found in this outgoing message.
Checked by AVG Free Edition.
Version: 7.5.488 / Virus Database: 269.13.39/1045 - Release Date: 02/10/2007
18.43