How to call python script from Groovy and the python script in turn calls another python script


I am trying to call the python script from groovy and passing some command line arguments along with the command.

The command I found to execute python code from groovy is:

Groovy code :

"python I:\Researchwork\ ‘myname’ ‘count’ ".execute()


In my python script file, I am taking the arguments and making a text file using this arguments. After this I am calling one more script file from the same file.

Code of python script:

import sys, optparse, os

import subprocess

if __name__ == '__main__':

    # Count the arguments

    open('file.txt', 'w').close()

    with open('file.txt', 'a') as file:        


"python 'file.txt' 'foldername' ", shell=True)

    print("Done") :

This file takes in the file.txt and foldername and does my work

Here file.txt is a file where I am storing the text and passing this file.txt name and the foldername to call my second python script file using

My problem is,

  • The groovy code is able to call the python script but the file.txt is not updating with the text from command line argument
  • The sub script in python script file is also not executing(i.e., file is not executing)

Please Kindly help me to solve the issue.

Thanks & Regards,

Hi @Jyothi_R,

I’ve never tried to call a Python script from Groovy before… But QuPath can be called directly from the command line and can run Groovy script from there as well. If you were to ‘translate’ your Python script to Groovy maybe this will be an easier and cleaner solution to run your tasks?

1 Like

My problem is solved.
Thanks for the ones who had a look at my problem to help me.

Thanks & Regards,

Could you please share the solution? I want to send an array from groovy to python script but unable to find a solution for implemention.

I have been working up to ask a big question about this, however recent events were… distracting.
I will probably write up something more complete on Sunday, but if you do not want anything complicated, you can run Python scripts using something like:

def cmdArray = ["F:/Anaconda/envs/cellpose/python.exe", "f:/"]
def proc = cmdArray.execute()
println "return code: ${ proc.exitValue()}"
println "stderr: ${proc.err.text}"
println "stdout: ${}"

The last lines are various ways to get information back out of the script, other than seeing the results (in my case, the QPTest file copied a file from one location to another).
To pass information to it, you need the python script to accept Args, but the format is mostly the same:

Here I was playing around with a script from the old QuPath forum:

//This script can be added to the end of very slow QuPath runs in order to alert the user by email.  
//Email address is set in the python file

// Get the imageData & server
String path = getCurrentImageData().getServer().getPath()

//As long as python files can be run at the command line interface using "python", the following should work for the location of a python script

def cmdArray = ["python", "c:\\Python\\", path]

It would then use the path for the current image to do things. I haven’t gotten the emailer to work, but I was able to verify that the argument was being passed to the python script.

Best of luck! Let me know if you figure anything else out. Right now my main problem is figuring out how to call something complex, like a long command line activation of CellPose. I can run CellPose just fine, batch, in the Anaconda window, but I cannot perform the same command through QuPath.