# Python -called from subprocess- is unable to import SimpleITK

**URL:** <https://discourse.slicer.org/t/python-called-from-subprocess-is-unable-to-import-simpleitk/4437>\
**Category:** Support\
**Tags:** python\
**Created:** [October 17, 2018, 3:01pm UTC](https://discourse.slicer.org/t/python-called-from-subprocess-is-unable-to-import-simpleitk/4437 "2018-10-17T15:01:59Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![abertelsen](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.slicer.org/abertelsen/32/8788_2.png) [@abertelsen](https://discourse.slicer.org/u/abertelsen)\
**Post date:** [October 17, 2018, 3:01pm UTC](https://discourse.slicer.org/t/python-called-from-subprocess-is-unable-to-import-simpleitk/4437/1 "2018-10-17T15:01:59Z")

</div>

Hello everybody,

I want to call a python module from Slicer, but I need to call it through the _subprocess_ module and not directly from Slicer’s Python environment. This is because my module uses scipy, which is not available in Slicer’s Pyhton (my module also depends on SimpleITK, which also brings a problem as you may see below).

I have tried to call my module using `subprocess.check_call` but it fails as scipy is not avaiable. In fact, a single call to `import scipy` fails:

```
>>> import subprocess
>>> python_path = 'C:/Users/abertelsen/AppData/Local/Continuum/Anaconda2/python.exe'
>>> subprocess.check_call([python_path, '-c', 'import scipy'])
Traceback (most recent call last):
  File "<console>", line 1, in <module>
  File "C:\Program Files\Slicer 4.8.1\lib\Python\Lib\subprocess.py", line 219, in check_output
    raise CalledProcessError(retcode, cmd, output=output)
CalledProcessError: Command '['C:/Users/abertelsen/AppData/Local/Continuum/Anaconda2/python.exe', '-c', 'import scipy']' returned non-zero exit status 1

```

I though that the problem may be related to the environment variables, so I added the `-E` option to python -which forces Python to ignore all environment variables that begin with ‘PYTHON\*’- and it seemed to work:

```
>>> subprocess.check_call([python_path, '-E', '-c', 'import scipy'])
0

```

And now comes the ‘funny’ part: adding the `-E` option prevents the import of SimpleITK

```
>>> subprocess.check_call([python_path, '-E', '-c', 'import SimpleITK'])
Traceback (most recent call last):
  File "<console>", line 1, in <module>
  File "C:\Program Files\Slicer 4.8.1\lib\Python\Lib\subprocess.py", line 186, in check_call
    raise CalledProcessError(retcode, cmd)
CalledProcessError: Command '['C:/Users/abertelsen/AppData/Local/Continuum/Anaconda2/python.exe', '-E', '-c', 'import SimpleITK']' returned non-zero exit status -1073740777

```

…but removing the `-E` option solves the problem

```
>>> subprocess.check_call([python_path, '-c', 'import SimpleITK'])
0

```

Therefore, if I add the `-E` option I am unable to import SimpleITK and, if I remove it, I become unable to import scipy. In both cases, I am unable to run my script from Slicer ☹

My Python environment is Anaconda2 (with Python 2.7.13, which is the same Python version that Slicer 4.8.1 uses). I do not get the same problem calling the commands above from the Anaconda Prompt: in fact, all cases above work when called from there.

Some info on my environment:

- Slicer 4.8.1 (with Python 2.7.13)
- Anaconda2 4.4.0 64-bits (with Python 2.7.13)
- Windows 10 (64 bits)

Any help would be greatly appreciated!

Best wishes,  
Álvaro B.

**PS** I have noticed that Slicer’s Python has been compiled with MSC v.1800, whereas Anaconda’s Python has been compiled with MSC v.1500. Do you believe that the problem may be related to the compiler? I doubt it, but I wanted to add this point.

---

<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:** [October 17, 2018, 3:04pm UTC](https://discourse.slicer.org/t/python-called-from-subprocess-is-unable-to-import-simpleitk/4437/2 "2018-10-17T15:04:32Z")

</div>

You can use `slicer.util.startupEnvironment()` to get an unmodified system environment. See details in this post: [Subprocess call in Python interpreter results in memory corruption](https://discourse.slicer.org/t/subprocess-call-in-python-interpreter-results-in-memory-corruption/919/17)

---

<div class="post-metadata">

**Author:** ![abertelsen](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.slicer.org/abertelsen/32/8788_2.png) [@abertelsen](https://discourse.slicer.org/u/abertelsen)\
**Post date:** [October 17, 2018, 3:24pm UTC](https://discourse.slicer.org/t/python-called-from-subprocess-is-unable-to-import-simpleitk/4437/3 "2018-10-17T15:24:02Z")

</div>

Using `slicer.util.startupEnvironment` still produces an error

`subprocess.check_output([python_path, '-c', 'import SimpleITK'], env=slicer.util.startupEnvironment())`

`Traceback (most recent call last): File "<console>", line 1, in <module> File "C:\Program Files\Slicer 4.8.1\lib\Python\Lib\subprocess.py", line 212, in check_output process = Popen(stdout=PIPE, *popenargs, **kwargs) File "C:\Program Files\Slicer 4.8.1\lib\Python\Lib\subprocess.py", line 390, in __init__ errread, errwrite) File "C:\Program Files\Slicer 4.8.1\lib\Python\Lib\subprocess.py", line 640, in _execute_child startupinfo) TypeError: environment can only contain strings`

I get the same error calling Python with or without the `-E` option.

---

<div class="post-metadata">

**Author:** ![ihnorton](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.slicer.org/ihnorton/32/9_2.png) [@ihnorton](https://discourse.slicer.org/u/ihnorton)\
**Post date:** [October 17, 2018, 3:51pm UTC](https://discourse.slicer.org/t/python-called-from-subprocess-is-unable-to-import-simpleitk/4437/4 "2018-10-17T15:51:27Z")

</div>

If you installed SimpleITK with pip rather than conda, then it might rely on something being in PATH.

A hacky quick-fix for the `TypeError` might be passing a modified environment, `env = {str(k): str(v) for k,v in slicer.util.startupEnvironment().items()}`.

---

<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:** [October 17, 2018, 5:41pm UTC](https://discourse.slicer.org/t/python-called-from-subprocess-is-unable-to-import-simpleitk/4437/5 "2018-10-17T17:41:26Z")

</div>

I just see now that you use Slicer-4.8.1. This issue has been fixed in recent Slicer-4.9.x versions.

---

<div class="post-metadata">

**Author:** ![abertelsen](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.slicer.org/abertelsen/32/8788_2.png) [@abertelsen](https://discourse.slicer.org/u/abertelsen)\
**Post date:** [October 18, 2018, 7:28am UTC](https://discourse.slicer.org/t/python-called-from-subprocess-is-unable-to-import-simpleitk/4437/6 "2018-10-18T07:28:49Z")

</div>

Calling `subprocess.check_call` using the modified `env` did the trick. Thank you Andras and Isaiah for your replies!

Best wishes,  
Álvaro B.

---

<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:** [October 18, 2018, 11:46am UTC](https://discourse.slicer.org/t/python-called-from-subprocess-is-unable-to-import-simpleitk/4437/7 "2018-10-18T11:46:27Z")

</div>

Forced conversion to string was implemented in nightly (Slicer-4.9.x) version several months ago. So, any recent nightly builds should work as is.
