BookmarkSubscribeRSS Feed
ytcaudience
Fluorite | Level 6

Hi,

I was wondering how one can check if python in enabled from SAS code. (lockdown enable_ams=PYTHON; )

If I run the following function below in a SAS code, I get an error because python has not been enabled on the system I’m using.

Proc python;

Submit;

Endsubmit;

The resulting error is: “sas ERROR: Failed to create Python subprocess.”

The question is: how can I check if python is enabled before calling “proc python”?

This will help to handle the error in production.

Thanks.

2 REPLIES 2
sastpw
SAS Employee

Hey, so there a a couple different things that may help, depending upon what's not set up right for the proc to work. The first thing I do to see if the correct options are set and configured (PROC_PYPATH, lockdown, and if lockdown, both PYTHON and SOCKET being enabled) is to run the following code.

 

data _null_;
x= sysget('PROC_PYPATH');  put 'PROC_PYPATH=' x;
x= sysget('VIYA_LOCKDOWN_USER_METHODS');  put 'VIYA_LOCKDOWN_USER_METHODS=' x;
run;
proc options option=lockdown;run;

On a system here, that's set up correctly, I get the following:

80   data _null_;
81   x= sysget('PROC_PYPATH');  put 'PROC_PYPATH=' x;
82   x= sysget('VIYA_LOCKDOWN_USER_METHODS');  put 'VIYA_LOCKDOWN_USER_METHODS=' x;
83   run;
PROC_PYPATH=/opt/sas/viya/home/sas-pyconfig/default_py/bin/python3
VIYA_LOCKDOWN_USER_METHODS=PYTHON PYTHON_EMBED SOCKET
NOTE: DATA statement used (Total process time):
      real time           0.00 seconds
      cpu time            0.00 seconds
      
84   proc options option=lockdown; run;
    SAS (r) Proprietary Software Release V.04.00  TS1M0
LOCKDOWN          Specifies that access to files and certain SAS features will be restricted. This feature is only applicable for 
                   a SAS session executing in a batch or server processing mode.
NOTE: PROCEDURE OPTIONS used (Total process time):
      real time           0.00 seconds
      cpu time            0.01 seconds

For cases where something isn't configured right, the error can vary. If LOCKDOWNis set, but PYTHON isn't enabled, the error will be something to the effect of saying the proc can't be run because PYTHON isn't enabled (it won't try to start a subprocess). If SOCKET isn't enabled, but the proc runs, the sd2df() and df2sd() methods will fail and the log from those will have an equivalent error about SOCKET not being enabled while in LOCKDOWN mode. For cases where Python can't start (the path isn't set or wrong, the deployment has issues, whatever), then you'll see something like the error you showed about failing to create the subprocess.

 

The only way I see to programmatically check, is that the &SYSERRTEXT automatic marco variable has a message in it, while it is blank (empty) when the proc succeded.

 

proc python;quit;
%put &syserr &syserrortext &sysrc &syscc &syswarningtext;

options set=PROC_PYPATH='/bad/path'; /* make it fail */
proc python restart;quit; %put &syserr &syserrortext &sysrc &syscc &syswarningtext;
87   proc python;quit;
NOTE: Python initialized.
Python 3.8.13 (default, Jul 21 2022, 07:19:43) 
[GCC 8.5.0 20210514 (Red Hat 8.5.0-10)] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>>
>>> 
88   %let SAS_workpath = %sysfunc(pathname(work));
NOTE: PROCEDURE PYTHON used (Total process time):
      real time           1.13 seconds
      cpu time            0.05 seconds
      
89   %put &syserr &syserrortext &sysrc &syscc &syswarningtext;
0  0 0
90   
91   options set=PROC_PYPATH='/bad/path';  /* make it fail */
92   
93   proc python restart;quit;
NOTE: Python terminated.
NOTE: Previous Python state destroyed.
ERROR: Failed to create Python subprocess.
NOTE: Python terminated.
NOTE: PROCEDURE PYTHON used (Total process time):
      real time           0.02 seconds
      cpu time            0.00 seconds
      
94   %put &syserr &syserrortext &sysrc &syscc &syswarningtext;
0 Failed to create Python subprocess. 0 0
95   

So, you could programmatically check by seeing is &SYSERRTEXT is empty or not after trying 'proc python;quit;'. I wouldn't check for a specific error message, just blank or not. 

 

I hope some of this helps,

Tom

 

sastpw
SAS Employee

Looking into this a little deeper, I found a bug in the proc where these early failures are exiting the proc without a failure rc. I've fixed those cases and now &SYSERR is correctly being set when there is a failure. So, once this propagates into whatever release it gets in, you will be able to check &SYSERR to see if the proc failed or not. SYSERR gets set (reset) after each step, while SYSERRTEXT doesn't get reset. That's not the proc behavior, but rather SAS's way it sets, or doesn't, these various macro variables.

But, that should be a much cleaner way to programmatically check.

 

Tom

  1?
  2? proc python;quit;
%put &syserr &syserrortext &sysrc &syscc &syswarningtext;

options set=PROC_PYPATH='/bad/path';  /* make it fail */

proc python restart;quit;
%put &syserr &syserrortext &sysrc &syscc &syswarningtext;

options set=PROC_PYPATH='/usr/bin/python3.5';  /* make it work again */

proc python restart;quit;
%put &syserr &syserrortext &sysrc &syscc &syswarningtext;


NOTE: Python initialized.
Python 3.5.6 (default, Nov 16 2018, 15:50:39)
[GCC 4.4.7 20120313 (Red Hat 4.4.7-23)] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>>
NOTE: PROCEDURE PYTHON used (Total process time):
      real time           1.97 seconds
      cpu time            0.07 seconds


  4?
0  0 0

  8?
NOTE: Python terminated.
NOTE: Previous Python state destroyed.
ERROR: Failed to create Python subprocess.
NOTE: Python terminated.
ERROR: Launch Failed. Cannot execute program "/bad/path", error = 2 (No such
       file or directory).
ERROR: The specified executable module either cannot be located or it is not
       a valid executable.
ERROR: Failed to launch Python sub-process in tkpy extension.
NOTE: The SAS System stopped processing this step because of errors.
NOTE: PROCEDURE PYTHON used (Total process time):
      real time           0.07 seconds
      cpu time            0.01 seconds


  9?
1012 Failed to launch Python sub-process in tkpy extension. 0 1012

 13?
NOTE: Python initialized.
Python 3.5.6 (default, Nov 16 2018, 15:50:39)
[GCC 4.4.7 20120313 (Red Hat 4.4.7-23)] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>>
NOTE: PROCEDURE PYTHON used (Total process time):
      real time           1.10 seconds
      cpu time            0.01 seconds


 15?
0 Failed to launch Python sub-process in tkpy extension. 0 1012

 17?

sas-innovate-2024.png

Available on demand!

Missed SAS Innovate Las Vegas? Watch all the action for free! View the keynotes, general sessions and 22 breakouts on demand.

 

Register now!

How to choose a machine learning algorithm

Use this tutorial as a handy guide to weigh the pros and cons of these commonly used machine learning algorithms.

Find more tutorials on the SAS Users YouTube channel.

Discussion stats
  • 2 replies
  • 1960 views
  • 2 likes
  • 2 in conversation