Question Akshay Pandey · Oct 25, 2021

Running Python script from terminal vs studio

I have created on class method which execute below command

From terminal : _System account works fine where code=0

From Studio : _System account give syntax error where code=1

set code = $zf(-1,"python C:\keys\GetToken.py")

w !, code

Product version: IRIS 2020.1
$ZV: IRIS for Windows (x86-64) 2020.1 (Build 215U) Mon Mar 30 2020 20:14:33 EDT

Comments

Robert Cemper · Oct 25, 2021

does it change using %cde instead of code  as variable ?

0
Akshay Pandey  Oct 25, 2021 to Robert Cemper

nope...code =0 means sucess

code =1 means syntax error

I am not sure how same method is behaving differently

0
Robert Cemper · Oct 25, 2021

studio is not a real terminal with an isolated session context.

0
Akshay Pandey  Oct 25, 2021 to Robert Cemper

Classmethod Test() {

set code $zf(-1,"python C:\keys\GetToken.py")

W ! , code

}

Classmethod Test2() {

set code $zf(-1,"echo hello>C:\keys\test.txt")

W ! , code

}

Studio Output Window

Do ##class(Test).Test()

1

Do ##class(Test).Test2()

0

Cache terminal

Do ##class(Test).Test()

0

Do ##class(Test).Test2()

0

this is what is happening

0
Robert Cemper  Oct 25, 2021 to Akshay Pandey

this looks like "python" is not recognized in the spawned process
You may try a full reference "C:\......python.exe...."   

0
Vic Sun  Oct 26, 2021 to Robert Cemper

Hello all,

I just wanted to emphasize this point by Robert, which I think is the most critical. Studio is not a real terminal. I understand it can be convenient to test small bits of code in the Studio window, but I would not expect everything to work.

0
Robert Cemper  Oct 26, 2021 to Vic Sun

Hi @Vic Sun , @Akshay Pandey :
Just to complete this discussion.

From Terminal I could run Py also from a JOB, and over CPIPE without problems.
From Studio Output all this FAILS.
Which makes clear that the process behind Studio is far away from being a normal
IRIS/Caché process and just good for editing and compiling.  FullStop.
I would even assume that running COS commands was not planned but rather an accident that happened 20 yerars ago.
 

0
Patrick Halloran · Oct 25, 2021

Hi Akshay,

Robert is correct.

$ZF(-1,{os command}) is used to call commands from the underlying operating system.

The shell that you execute won't know where to find the python executable.

We can see this by trying to print the Windows PATH environment variable (below) as you would normally be able to do in a Windows CMD shell:

As we can see above the CMD shell can see the environment variable %PATH% but if we do the same echo %PATH% in terminal we see nothing.  

Cheers,
Patrick

0
Robert Cemper  Oct 25, 2021 to Patrick Halloran

thanks for the clarification.   I had a similar experience with node.js before.
But was not so certain to know the reasoning.
yes
 

0
Akshay Pandey  Oct 26, 2021 to Robert Cemper

I have tried with full path as well still not working 

Studio
d $zf(-1,"whoami > c:\temp\whoami.txt")

nt authority\system    ( i dont know which user is this
 

Terminal 

d $zf(-1,"whoami > c:\temp\whoami.txt")

give local user

0
Robert Cemper  Oct 26, 2021 to Akshay Pandey

Just an idea:
I assumed that you use a local installation of IRIS
Is this correct ? 
Or does it run on some instance separate from your local WIN.
whoami is equivalent to echo %USERDOMAIN%\%USERNAME%

write $zf(-1,"hostname > c:\temp\hostname.txt")

may clarify this.
If it's not the same machine you need a PY installation this server

0
Akshay Pandey  Oct 26, 2021 to Robert Cemper

this is local installation

I did tried with complete python.exe path as well. 

but when -ZF(-1) running from studio its not working

0
Michel Bruyère  Oct 26, 2021 to Akshay Pandey

When you use 'Studio', you are recognised like an administrator (nt authority\system) and not a local user.
Don't use $ZF(-1) : $ZF(-1) is a deprecated function. All new code development should use $ZF(-100).
What about your python with the new recommendation?

0
Akshay Pandey  Oct 26, 2021 to Michel Bruyère

I tried with $ZF(-100) as well still I get code=1 (Syntax error)

same method while running from cache terminal works fine

whereas running from studio gives error

0
Robert Cemper  Oct 26, 2021 to Akshay Pandey

I spent quite a while on the subject of python. Also the asynchronous $ZF(-2) + error log

write $zf(-2,"C:\Users\cemper\AppData\Local\Microsoft\WindowsApps\pythonw3.9.exe <c:\temp\in.py 1>c:\temp\5out.txt 2>c:\temp\5er.txt")

And the error from WINDOWS was always: the program can not be executed
A further reason to use
Embedded Python!

in.pyw is simply

print("success")
exit()

 

0
Vitaliy Serdtsev · Nov 2, 2021

I did a small test locally (for Caché).

test.py:

print("success")
exit()

From terminal:

USER><FONT COLOR="#0000ff">w $zf</FONT><FONT COLOR="#000000">(-1,</FONT><FONT COLOR="#008000">"python C:\tmp\test.py"</FONT><FONT COLOR="#000000">)</FONT>
0

From Studio Output Window:

<FONT COLOR="#0000ff">w $zf</FONT><FONT COLOR="#000000">(-1,</FONT><FONT COLOR="#008000">"python C:\tmp\test.py"</FONT><FONT COLOR="#000000">)</FONT>
0

Make sure that your service for Caché/IRIS use an account with administrative rights:

0
Ben Spead  Nov 2, 2021 to Vitaliy Serdtsev

Small suggest - use $zf(-100) rather than $zf(-1) as it is more secure and $zf(-1) is not supposed to be used anymore (it is kept in place for backward compatibility but it really is deprecated for use in new code)

0