I want to run a custom python script from slicer in a custom virtual environment.
I tried:
-
slicer.util.launchConsoleProcess()
, as suggested here, but I couldn’t figure out how to specify my custom venv - I tried using
subprocess.call(cmd, shell=True, executable='/bin/bash')
, wherecmd = "source path/to/venv/bin/activate; python3 path/to/script.py
(I tried this with.
instead of source or without source but nothing worked)
I also tried a somewhat convoluted way that was the closest to working: subprocess.run(command_line, env=slicer.util.startupEnvironment())
, where command_line=["path/to/venv/bin/python3.7", "path/to/intermediate/script.py"]
and the intermediate python file contains an os.system call with the venv and ‘real’ script specified
- I wrote that it partially worked because I get a return code of 0, and the loading circle in slicer appears for a moment - so I’m guessing the ‘real’ script started but then closed after a moment
(the script I want to run is a script that continuously sends data to Slicer, where my extension then uses this data to update markups)
It seemed to me like I tried all the options that If found here on Discourse, but maybe I missed something.