# Subprocess using conda python

**URL:** <https://discourse.slicer.org/t/subprocess-using-conda-python/8409>\
**Category:** Support\
**Created:** [September 12, 2019, 6:29pm UTC](https://discourse.slicer.org/t/subprocess-using-conda-python/8409 "2019-09-12T18:29:29Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![ghnguyen](https://avatars.discourse-cdn.com/v4/letter/g/c6cbf5/32.png) [@ghnguyen](https://discourse.slicer.org/u/ghnguyen)\
**Post date:** [September 12, 2019, 6:29pm UTC](https://discourse.slicer.org/t/subprocess-using-conda-python/8409/1 "2019-09-12T18:29:29Z")

</div>

Hi,  
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 [https://discourse.slicer.org/t/interacting-with-external-python/4897](https://discourse.slicer.org/t/interacting-with-external-python/4897) but could not resolve the problem. My conda environment has all the packages I need. I was searching on the web and found [this](https://www.scivision.dev/python-calling-python-subprocess/) 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).

---

<div class="post-metadata">

**Author:** ![lassoan](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.slicer.org/lassoan/32/13_2.png) [@lassoan](https://discourse.slicer.org/u/lassoan)\
**Post date:** [September 12, 2019, 9:09pm UTC](https://discourse.slicer.org/t/subprocess-using-conda-python/8409/2 "2019-09-12T21:09:41Z")

</div>

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: [https://github.com/lassoan/SlicerPythonCLIExample](https://github.com/lassoan/SlicerPythonCLIExample).

---

<div class="post-metadata">

**Author:** ![ghnguyen](https://avatars.discourse-cdn.com/v4/letter/g/c6cbf5/32.png) [@ghnguyen](https://discourse.slicer.org/u/ghnguyen)\
**Post date:** [September 13, 2019, 3:10pm UTC](https://discourse.slicer.org/t/subprocess-using-conda-python/8409/3 "2019-09-13T15:10:43Z")

</div>

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 [https://pypi.org/project/pyimagej/](https://pypi.org/project/pyimagej/) 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.

---

<div class="post-metadata">

**Author:** ![lassoan](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.slicer.org/lassoan/32/13_2.png) [@lassoan](https://discourse.slicer.org/u/lassoan)\
**Post date:** [September 13, 2019, 3:11pm UTC](https://discourse.slicer.org/t/subprocess-using-conda-python/8409/4 "2019-09-13T15:11:53Z")

</div>

What is the output if you use `subprocess.check_output`?

---

<div class="post-metadata">

**Author:** ![ghnguyen](https://avatars.discourse-cdn.com/v4/letter/g/c6cbf5/32.png) [@ghnguyen](https://discourse.slicer.org/u/ghnguyen)\
**Post date:** [September 13, 2019, 3:33pm UTC](https://discourse.slicer.org/t/subprocess-using-conda-python/8409/5 "2019-09-13T15:33:31Z")

</div>

> subprocess.check\_output([“C:/Users/gnguyen/Anaconda/envs/imagej/python.exe”, “C:/Users/gnguyen/Desktop/3dSlicerExtension/Macro\_Test/pyimagej.py”], 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\subprocess.py”, line 336, in check\_output  
> \*\*kwargs).stdout  
> File “C:\Users\gnguyen\AppData\Local\NA-MIC\Slicer 4.11.0-2019-09-11\lib\Python\Lib\subprocess.py”, 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/pyimagej.py’]’ 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.

---

<div class="post-metadata">

**Author:** ![lassoan](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.slicer.org/lassoan/32/13_2.png) [@lassoan](https://discourse.slicer.org/u/lassoan)\
**Post date:** [September 13, 2019, 4:42pm UTC](https://discourse.slicer.org/t/subprocess-using-conda-python/8409/6 "2019-09-13T16:42:40Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![ghnguyen](https://avatars.discourse-cdn.com/v4/letter/g/c6cbf5/32.png) [@ghnguyen](https://discourse.slicer.org/u/ghnguyen)\
**Post date:** [September 13, 2019, 4:56pm UTC](https://discourse.slicer.org/t/subprocess-using-conda-python/8409/7 "2019-09-13T16:56:44Z")

</div>

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!
