General Approach to Start an External Application

Everything OAD-related that won't fit in the other categories: share general aspects of macro programming and discuss the OAD environment
somayeh zeraati
Posts: 3
Joined: Mon Apr 11, 2022 4:58 am

Executing external python script from ZEN

Post by somayeh zeraati »

Hello everyone,

I'm working one ZEN automation. I wonder how I can execute a python script from ZEN. I have used these command lines but it didn't work!
##################
from System.Diagnostics import Process
pythonexe = r'anaconda3\python.exe'
#script = r'Documents\test.py'
script1 = r'Documents\automated-acquisition\scripts\detection.py'
app = Process()
app.StartInfo.WorkingDirectory = r'C:\Users\LSM User'
app.StartInfo.FileName = pythonexe
app.StartInfo.Arguments = script1
app.StartInfo.UseShellExecute = True
app.Start()
app.WaitForExit()
####################
I will appreciate any help. thank you so much.
CarlZeissMicroscopy3
Posts: 180
Joined: Wed May 20, 2020 10:10 am

Re: General Approach to Start an External Application

Post by CarlZeissMicroscopy3 »

Hello somayeh zeraati,

let us investigate your problem step by step:

Just to be sure we use absolute paths and not relative ones.
(If everything works fine you can change the example to app.StartInfo.WorkingDirectory with relative paths)

Code: Select all

from System.Diagnostics import Process
pythonexe = r'C:\FullPathToPythonExe\python.exe'
script = r'C:\FullPathToTest\Test.py'
app = Process()
app.StartInfo.FileName = pythonexe
app.StartInfo.Arguments = script
app.StartInfo.UseShellExecute = True
app.Start()
app.WaitForExit()
with Test.py just writing a Test.txt-file like

Code: Select all

text_file = open(r"C:\FullPathToTest\Test.txt", "w")
text_file.write("Hello")
text_file.close()
Running the macro calls Test.py to generate the file Test.txt with a Hello in it.

This works perfect on my computer!

If this does not work on your system, please generate a new Text-Document and name it RunText.txt
Insert

Code: Select all

echo off
C:\FullPathToPythonExe\python.exe "C:\FullPathToTest\Test.py"
pause
into it, save and close it and finally rename RunTest.txt to RunTest.bat
A DoubleClick on the RunTest.bat should generate Test.txt with a Hello in it.

If this is still not the case, you should check the paths an look for misspellings.
In case you cannot get RunTest.bat to execute successfully chances are high that your python installation is somehow broken.

Good luck!
somayeh zeraati
Posts: 3
Joined: Mon Apr 11, 2022 4:58 am

Re: General Approach to Start an External Application

Post by somayeh zeraati »

Hello,

Thank you very much for the quick reply.
I tried it, and it works on my computer. but still I can not get any output from my own python code(.py file)! the code gets images and analysises them, then should give me an output (which is a .pkl file contains coordinations of segmented nucleus).
the .py file works in my computer (running time is about 2 minuets! ), but it couldn't execute from macro!
CarlZeissMicroscopy3
Posts: 180
Joined: Wed May 20, 2020 10:10 am

Re: General Approach to Start an External Application

Post by CarlZeissMicroscopy3 »

Hello somayeh zeraati,

it is not so easy to understand why 'some code' in a .py file is not running.

Maybe it is a good idea to reduce your code in the .py file to a minimalistic verson and/or report each step like:

Code: Select all

text_file = open(r"C:\FullPathToTest\Test.txt", "w")
text_file.write("Starting")
...
text_file.write("Fist step is ok")
....
text_file.write("Second step has " + str(items) + " items")
...
text_file.write("...")
...
text_file.close()
This way it should be possible to find the line(s) of code where the problem(s) occur(s).
somayeh zeraati
Posts: 3
Joined: Mon Apr 11, 2022 4:58 am

Re: General Approach to Start an External Application

Post by somayeh zeraati »

Hello, thanks a lot.

Well, macro itself doesn't give me any error when I run the .py file! just no output.
So I'll try to make the code short as much as possible, then write to you the result.
Post Reply