Interacting with external python

I am trying to build on this example in:
https://www.slicer.org/wiki/Documentation/Nightly/Developers/Python_scripting#How_to_run_Python_script_using_a_non-Slicer_Python_environment

This

python_path = "C:/Users/amaga/AppData/Local/Continuum/anaconda3/python"
command_line = [python_path, "-c", "import pyRserve; conn = pyRserve.connect(); print(conn.eval('3 + 5'))"]
from subprocess import check_output
command_result = check_output(command_line, env=slicer.util.startupEnvironment())
print(command_result)

fails with error.

Traceback (most recent call last):
File “<console>”, line 1, in <module>
File “C:\Program Files\Slicer 4.11.0-2018-11-02\lib\Python\Lib\subprocess.py”, line 219, in check_output
raise CalledProcessError(retcode, cmd, output=output)
CalledProcessError: Command ‘[‘C:/Users/amaga/AppData/Local/Continuum/anaconda3/python’, ‘-c’, “import pyRserve; conn = pyRserve.connect(); print(conn.eval(‘3 + 5’))”]’ returned non-zero exit status 120

But in the interactive python console import pyRserve; conn = pyRserve.connect(); print(conn.eval(‘3 + 5’))
returns the correct value. So I know Rserve is working.

What is the correct way of passing bunch of commands to the external python in this context?

After first starting the R server from a separate R interpreter (details here), I wasn’t able to reproduce the issue.

himm,

On a different windows computer, I followed your updated pip install example and issued the command
"“c:\Program Files\Slicer 4.10.0\bin\PythonSlicer.exe” -m pip install pyRserve
as admin.
As far as I can tell, pyRserve installed correctly (although there was a warning about path (truncated output below)

Successfully built pyRserve
Installing collected packages: attrs, pluggy, scandir, pathlib2, funcsigs, colorama, atomicwrites, more-itertools, py, pytest, pyRserve
The scripts py.test.exe and pytest.exe are installed in ‘C:\Program Files\Slicer 4.10.0\lib\Python\Scripts’ which is not on PATH.
Consider adding this directory to PATH or, if you prefer to suppress this warning, use --no-warn-script-location.
Successfully installed atomicwrites-1.2.1 attrs-18.2.0 colorama-0.4.1 funcsigs-1.0.2 more-itertools-4.3.0 pathlib2-2.3.2 pluggy-0.8.0 py-1.7.0 pyRserve-0.9.1 pytest-4.0.1 scandir-1.9.0

then I started the Rserve on my local R (3.5.0) and tested its connection with anaconda by issuing

import pyRserve; conn = pyRserve.connect(); print(conn.eval(‘3 + 5’))

and got the right result.

then I opened Slicer 4.10, and typed the script example above (path is slightly different on this machine).

python_path = “C:/Users/murat/anaconda3/python”
command_line = [python_path, “-c”, “import pyRserve; conn = pyRserve.connect(); print(conn.eval(‘3 + 5’))”]
from subprocess import check_output
command_result = check_output(command_line, env=slicer.util.startupEnvironment())
print(command_result)

and still got the exact error message as above.

Should I add the C:\Program Files\Slicer 4.10.0\lib\Python\Scripts’ to system path as suggested by install? I was wary of that messing up other things in Slicer. WHat else I might be missing?

Based on the snippet you are trying to execute, pyRserve should be installed within anaconda not within Slicer.

Right, there was no point in installing into Slicer (getting late here).
Nevertheless, it was installed in anaconda.

Whatever it was, it doesn’t matter anymore. Running it through Slicer’s pyRserve worked fine, which is more important anyways.
Thanks for the help!