Run a Python script as subprocess from Slicer

Hi,
can you please tell me how I can run a python script as a subprocess from Slicer?
I would like to lauch a python file from the logic of my module.

Thank you

Maybe slicer.util.launchConsoleProcess could work for this

You can also run your Python script as a CLI module. Slicer automatically generates a GUI for specifying inputs and outputs; and you can also run the script as any other CLI module. See a complete example here: GitHub - lassoan/SlicerPythonCLIExample: Example extension for 3D Slicer that demonstrates how to make a Python script available as a CLI module

1 Like

Thank you for your reply.

I noticed that the python script runned as a CLI module is much slower than if I run it from an external command prompt.

Do you know any possible reason?

Thank you

You can get definitive answer by using a Python profiler, but a likely root cause is excessive logging. See more details here:

Thank you.

I also tried to run a simple os.system from the logic of my Slicer module:

import os
.
.
.

path_to_python = "python_path//"
os.system(f"{path_to_python}python.exe myscript.py")

but I am getting this error:

module 'os' has no attribute 'add_dll_directory'

While if I do the same from a Windows command prompt it works well:

import os
path_to_python = "python_path//"
os.system(f"{path_to_python}python.exe myscript.py")

Do you know the reason for this behaviour?

Thank you

os.system is a very rudimentary tool, you cannot control the used environment, output redirections, ownership of thr process, etc. Use subprocess instead. Set example here.

There is also the slicer.util.launchConsoleProcess function that conveniently redirects outputs, sets the environment, hides the console, gives access to the process so that you can log its outputs or if user requests then terminate the process.

If you want to run Python then you can use Slicer’s Python environment as shown here.

Hi Andras,
I have created a slicerpythonscriptedcli module. Is it possible to call subprocess_check_out to run a program using anaconda.

I am using some libraries that are installed in an enviornment in anaconda thats why I am calling the external python to run a program.

Please let me know how can i do it.

I receive the following error:

Traceback (most recent call last):
File “/home/useradmin/Slicer-4.11.20210226-linux-amd64/Scheduler/scheduler/scheduler.py”, line 68, in
main(sys.argv[1], float(sys.argv[2]), sys.argv[3], sys.argv[4], sys.argv[5])
File “/home/useradmin/Slicer-4.11.20210226-linux-amd64/Scheduler/scheduler/scheduler.py”, line 50, in main
command_result = check_output(command_line, env=slicer.util.startupEnvironment())
File “/home/useradmin/Slicer-4.11.20210226-linux-amd64/bin/Python/slicer/util.py”, line 106, in startupEnvironment
startupEnv = slicer.app.startupEnvironment()
AttributeError: module ‘slicer’ has no attribute ‘app’

regards,
Saima

slicer.util.startupEnvironment() requires that you run the script using ./Slicer executable (and not using ./PythonSlicer executable).

You could get the startup environment by calling the ctkAppLauncherEnvironment::environment(0) method in C++, but the only Python-wrapped class that exposes this method is qSlicerCoreApplication. If you start your Python script using ./PythonSlicer then no Qt application class is created, so you would need to add a new C++ class that allows you to call this function.

It would be much simpler to install the libraries using PyPI into Slicer’s Python environment. What are these libraries?

Its a toolkit, bratstoolkit for tumour segmentation.

regards,
Saima

Hi andras,
can I run check_output or run without env variable and run the process outside the slicer in an anaconda enviornment and get the results and avoid running into error due to startupenviornment.

At this stage I have to not use anything from slicer but after the processing the output is a volume(.nrrd), which I will export and do some processing on it.

regards,
Saima Safdar

You can pip install it in Slicer’s Python environment (PythonSlicer.exe -m pip install BraTS-Toolkit). There should be no need to mess with anaconda.

There is a problem that this package pins a very specific version of SimpleITK (and a couple of other Python packages), which makes Slicer remove the special SimpleITK distribution that is bundled with Slicer, so modules that rely on SimpleITK may stop working. See Did sitkUtils.PullVolumeFromSlicer() break with Slicer 5.2? - #9 by lassoan for more details.

Another bad thing about this bratstoolkit is that it comes with the strictest possible copyleft license. If you use this code then you must make all other code in the same project public. Probably you can find other segmentation toolkits that provide similar features and do not come with such heavy restrictions.

Hi Andras,
For the bratstoolkit, may be I could run it outside the slicer and get the results back into the slicer using the subprocess command and make the scripted module.

Is this ok? because if i dont install within slicer I will not have the SimpleITK issue.
The only thing I need for scripted module is how cani display the output or errors like in the
pythonCLi based module. That is a desperate post.

Can you suggest some other tumour segmentation toolkits ?

regards,
Saima