I try to use subprocess.call to run an external application in the python interactor of 3D Slicer but failed! Error message is “terminate called after throwing an instance of ‘H5::DataSpaceIException’”
However, when I run the python in the terminal and input the same command, the application works!
Is there any difference between the two pythons?
Thanks very much!
subprocess.call should generally work, however, it will block Slicer (GUI will freeze) until the call completes (you can use Popen or Slicer’s CLI infrastructure instead for asynchronous calls). I just tried subprocess.call(['sleep', '10']) on linux with a recent nightly, and it worked fine.
Please show a minimal example if you can – maybe someone will notice a problem.
The crash is probably because your application tries to use Slicer’s ITK, VTK, HDF5, etc. libraries. If you want to use the default system environment, you can retrieve it using slicer.util.startupEnvironment().
from subprocess import check_output
check_output(
["/usr/bin/python3", "-c", "print('hola')"],
env=slicer.util.startupEnvironment())
'hola\n'
@ihnorton Thanks for your reply. I just try to call a function of ANTs for image registration! @lassoan Thanks! It works according to your suggestion. In addition, shell should be set True. Otherwise, it does not work. I don’t know why and there is no any information output to the terminal!
Hi andras,
I am extactly doing the same but getting error.
Traceback (most recent call last):
File "<console>", line 1, in <module>
File "C:\Program Files\Slicer 4.10.2\lib\Python\Lib\subprocess.py", line 212, in check_output
process = Popen(stdout=PIPE, *popenargs, **kwargs)
File "C:\Program Files\Slicer 4.10.2\lib\Python\Lib\subprocess.py", line 390, in __init__
errread, errwrite)
File "C:\Program Files\Slicer 4.10.2\lib\Python\Lib\subprocess.py", line 640, in _execute_child
startupinfo)
WindowsError: [Error 2] The system cannot find the file specified
The message indicates that the path to the executable is incorrect. So that we can better understand the problem, could you share the code leading to this exception ?
command_line=["F:\anaconda3\python.exe","-c", "print('hola')"]
from subprocess import run
command_result = run(command_line, env =
slicer.util.startupEnvironment())
print(command_result)
Traceback (most recent call last):
File "F:\Slicer 4.11.0-2019-07-06\bin\Python\slicer\ScriptedLoadableModule.py", line 196, in onReload
slicer.util.reloadScriptedModule(self.moduleName)
File "F:\Slicer 4.11.0-2019-07-06\bin\Python\slicer\util.py", line 769, in reloadScriptedModule
moduleName, fp, filePath, ('.py', 'r', imp.PY_SOURCE))
File "F:\Slicer 4.11.0-2019-07-06\lib\Python\Lib\imp.py", line 235, in load_module
return load_source(name, filename, file)
File "F:\Slicer 4.11.0-2019-07-06\lib\Python\Lib\imp.py", line 170, in load_source
module = _exec(spec, sys.modules[name])
File "<frozen importlib._bootstrap>", line 618, in _exec
File "<frozen importlib._bootstrap_external>", line 678, in exec_module
File "<frozen importlib._bootstrap>", line 219, in _call_with_frames_removed
File "F:/Slicer 4.11.0-2019-07-06/subprocess/subprocess/subprocess.py", line 134, in <module>
slicer.util.startupEnvironment())
File "F:\Slicer 4.11.0-2019-07-06\lib\Python\Lib\subprocess.py", line 403, in run
with Popen(*popenargs, **kwargs) as process:
File "F:\Slicer 4.11.0-2019-07-06\lib\Python\Lib\subprocess.py", line 709, in __init__
restore_signals, start_new_session)
File "F:\Slicer 4.11.0-2019-07-06\lib\Python\Lib\subprocess.py", line 997, in _execute_child
startupinfo)
FileNotFoundError: [WinError 2] The system cannot find the file specified