Subprocess.call does not work!

Hi all,

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!

Operating system: Centos 7
Slicer version: 4.8
Expected behavior:
Actual behavior:

Yes, Slicer’s python is embedded.

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'

See details in this post: Subprocess call in Python interpreter results in memory corruption

1 Like

@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!

A post was split to a new topic: Calling C++ from Python

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

Would you please suggest what am I doing wrong??

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 ?

I am simply checking it with print statement.

command_line = [r"F:\anaconda3\python3.exe","-c","print('helo')"]
from subprocess import check_output
command_result = check_output(command_line, env = slicer.util.startupEnvironment())

Hi,
Why am I unable to run the code below

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

You should do this instead:

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)

the error remain the same

I am unable to understand.

It could not find the file.

@Saima I get the error you are getting when the python path that you are targeting doesn’t actually exist.

Can you confirm that "F:\anaconda3\python.exe" actually exists?

yes I did installation again and the path was changed. I didnt check updated the path. now its running.

Thanks all

FYI, since backslash (\) is an escape character in Python, you cannot simply use it in a string literal in Python.

The easiest is to indicate that you specify a raw string by prepending an r character before the string. This is correct:

somePath = r"F:\someFolder\python.exe"

If you need to use a regular string then backslash can be entered by typing double-backslash. This is correct:

somePath = "F:\\someFolder\\python.exe"

You may also use Unix-type separators. This is correct:

somePath = "F:/someFolder/python.exe"
1 Like