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.