# Interacting with external python

**URL:** <https://discourse.slicer.org/t/interacting-with-external-python/4897>\
**Category:** Support\
**Created:** [November 27, 2018, 10:23pm UTC](https://discourse.slicer.org/t/interacting-with-external-python/4897 "2018-11-27T22:23:32Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![muratmaga](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.slicer.org/muratmaga/32/3622_2.png) [@muratmaga](https://discourse.slicer.org/u/muratmaga)\
**Post date:** [November 27, 2018, 10:23pm UTC](https://discourse.slicer.org/t/interacting-with-external-python/4897/1 "2018-11-27T22:23:32Z")

</div>

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](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?

---

<div class="post-metadata">

**Author:** ![jcfr](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.slicer.org/jcfr/32/17825_2.png) [@jcfr](https://discourse.slicer.org/u/jcfr)\
**Post date:** [November 28, 2018, 7:34am UTC](https://discourse.slicer.org/t/interacting-with-external-python/4897/2 "2018-11-28T07:34:13Z")

</div>

After first starting the R server from a separate R interpreter (details [here](http://www.rforge.net/Rserve/faq.html#start)), I wasn’t able to reproduce the issue.

---

<div class="post-metadata">

**Author:** ![muratmaga](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.slicer.org/muratmaga/32/3622_2.png) [@muratmaga](https://discourse.slicer.org/u/muratmaga)\
**Post date:** [November 28, 2018, 8:09am UTC](https://discourse.slicer.org/t/interacting-with-external-python/4897/3 "2018-11-28T08:09:47Z")

</div>

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?

---

<div class="post-metadata">

**Author:** ![jcfr](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.slicer.org/jcfr/32/17825_2.png) [@jcfr](https://discourse.slicer.org/u/jcfr)\
**Post date:** [November 28, 2018, 8:11am UTC](https://discourse.slicer.org/t/interacting-with-external-python/4897/4 "2018-11-28T08:11:38Z")

</div>

> [@muratmaga](#):
>
> WHat else I might be missing?

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

---

<div class="post-metadata">

**Author:** ![muratmaga](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.slicer.org/muratmaga/32/3622_2.png) [@muratmaga](https://discourse.slicer.org/u/muratmaga)\
**Post date:** [November 28, 2018, 8:13am UTC](https://discourse.slicer.org/t/interacting-with-external-python/4897/5 "2018-11-28T08:13:28Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![muratmaga](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.slicer.org/muratmaga/32/3622_2.png) [@muratmaga](https://discourse.slicer.org/u/muratmaga)\
**Post date:** [November 28, 2018, 8:14am UTC](https://discourse.slicer.org/t/interacting-with-external-python/4897/6 "2018-11-28T08:14:57Z")

</div>

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!
