How to execut code in local .py file in python gateway?

Primary tabs

Python, Open Exchange

who has tried "python gateway" in OpenExchange, could you please tell me how to  execute code in a local .py file? a cusotmer engineer ask me the question while I don't have time to study it yet. Thank you very much. 
 

  • 0
  • 0
  • 43
  • 2
  • 1

Answers

This should work for Python Gateway:

set filename = "/path/to/code.py"
set file = ##class(%Stream.FileCharacter).%New()
set sc = file.LinkToFile(filename)
set sc = ##class(isc.py.Main).ExecuteCode(file)

In addition to Eduard's answer, a short example based on sending Python's exec command to Python (assuming Python 3.*) to execute a Python script stored in a file:

(1)  Create a testfile.py in some folder that Python has access to, add the following lines to testfile.py:

f=open('disk:/path/to/your/folder/output.txt','w')
print('Hello Python Gateway',file=f)
f.close()

Save and close testfile.py

(2)  Open  IRIS Terminal, type and execute one by one the following commands:

do ##class(isc.py.util.Shell).Shell()

set sc=##class(isc.py.Main).SimpleString("exec(open('disk:/path/to/your/folder/testfile.py').read())",,,.sc)

(3) Check the output.txt file that has been created in your folder

Note that instead of:

do ##class(isc.py.util.Shell).Shell()

Python Shell can be entered by simply typing

zpy

In there you can just directly type python code:

exec(open('disk:/path/to/your/folder/testfile.py').read())

Or don't open the shell and call:

set sc=##class(isc.py.Main).SimpleString("exec(open('disk:/path/to/your/folder/testfile.py').read())",,,.sc)