Subprocess using conda python

I’m trying to use subprocess to get the output of another script running in conda environment since I want other python packages there.

subprocess.check_call([“C:/Users/gnguyen/Anaconda/envs/pytest/python.exe”, “-c”, “from skimage import io”],env=slicer.util.startupEnvironment())

This returns CalledProcessError: non-zero exit status 1. If I drop the -c and use a script, same thing happens. However, simple statements like “-c”, “print (‘hola’)” work fine and gives exit status 0. I followed the documentation as well as this thread but could not resolve the problem. My conda environment has all the packages I need. I was searching on the web and found this but not sure if that helps here. Is there anything I’m missing or how should I work around this problem? Thanks.
I’m using Windows 10 (64 bit) and I have tested on both Slicer 4.10.2 and 4.11.0 (version on 09/12).

It works well for me. Run it subprocess.check_output to see the error message, probably it is some trivial error (using wrong environment, scipy not installed, etc.).

Note that if you use recent Slicer Preview version then you can install skimage by pip_install('scipy scikit-image') and use it in Slicer. You can even run a Python script as a module in Slicer (with GUI generated from an XML file) - see example here:

1 Like

Thanks for the suggestion, I installed sciki-image and can keep working on my project for now. Pip installing scikit image in older versions is broken but you’re correct, the recent versions works fine.
I was looking for a way to use this in Slicer as well but could not do it because of pyjnius installation kept producing error when pip_install. Hence I resorted to the subprocess route which still is not working now as I described.

What is the output if you use subprocess.check_output?

subprocess.check_output([“C:/Users/gnguyen/Anaconda/envs/imagej/python.exe”, “C:/Users/gnguyen/Desktop/3dSlicerExtension/Macro_Test/”], env=slicer.util.startupEnvironment())

results in this:

Traceback (most recent call last):
File “”, line 1, in
File “C:\Users\gnguyen\AppData\Local\NA-MIC\Slicer 4.11.0-2019-09-11\lib\Python\Lib\”, line 336, in check_output
File “C:\Users\gnguyen\AppData\Local\NA-MIC\Slicer 4.11.0-2019-09-11\lib\Python\Lib\”, line 418, in run
output=stdout, stderr=stderr)
subprocess.CalledProcessError: Command ‘[‘C:/Users/gnguyen/Anaconda/envs/imagej/python.exe’, ‘C:/Users/gnguyen/Desktop/3dSlicerExtension/Macro_Test/’]’ returned non-zero exit status 120.

Catching the error with except clause would give the same thing, error.output is a blank string and error.returncode is 120.

If you use virtual environment then you must activate it before you can use it (if you installed non-native Python packages, such as pyimagej). Simply running python.exe executable is expected to fail with DLL loading errors.

You need to use conda run or some other techniques to instruct conda to run your script in a specific virtual environment. All these have nothing to do with Slicer but general anaconda behavior, so you should be able to find help and examples on the web.

By the way, this whole pyimagej idea of wrapping Java with a Python interface is quite messy. Probably you would be better off with using equivalent Python packages.

1 Like

I agree, our collaborators used that so I was just trying to be consistent but now I think scikit image is more than enough to recreate the results. Thanks Andras!