Python script works in bash, but not when called from R via system
<p>I use Ubuntu 16.04. I'm trying to run a simple python script from R. The script is</p>

<pre><code>    import numpy as np
    x=1
    print(x)
</code></pre>

<p>and is written in a file named code.py. It works fine if I call it in bash via</p>

<pre><code>    python3.5 code.py
</code></pre>

<p>However, when I call it in R via</p>

<pre><code>    system("python3.5 code.py",intern=TRUE)
</code></pre>

<p>I get a message that says that numpy was not found. Any idea why there is this difference and how I can fix this?</p>

<p>Thanks!</p>

<p><strong>UPDATE</strong></p>

<p>If I run a file with  </p>

<pre><code>     import sys 
     print(sys.path)
</code></pre>

<p>I get</p>

<pre><code>     [1] "['/home/user/Desktop', '/usr/lib/python35.zip', '/usr/lib/python3.5', '/usr/lib/python3.5/plat-x86_64-linux-gnu', '/usr/lib/python3.5/lib-dynload', '/usr/local/lib/python3.5/dist-packages', '/usr/lib/python3/dist-packages']" 
</code></pre>

<p>if I run the file from R, and   </p>

<pre><code>    ['/home/user/Desktop', '/home/user/anaconda3/lib/python35.zip', '/home/user/anaconda3/lib/python3.5', '/home/user/anaconda3/lib/python3.5/plat-linux', '/home/user/anaconda3/lib/python3.5/lib-dynload', '/home/user/anaconda3/lib/python3.5/site-packages', '/home/user/anaconda3/lib/python3.5/site-packages/Sphinx-1.4.1-py3.5.egg', '/home/user/anaconda3/lib/python3.5/site-packages/setuptools-23.0.0-py3.5.egg']
</code></pre>

<p>if I run the file from the command line.</p>

<p>The problem is that you have two versions of python3 on your computer: The system default (Ubuntu, I'm assuming), and the one you installed (Anaconda3).</p>

<p>When you run it from the command-line, you are using the Anaconda3 environment (which includes numpy and all the other anaconda modules). When you run it from R, it doesn't know to use the Anaconda environment, and so it just uses your default python paths (which doesn't include numpy).</p>

<p>To fix this, invoke your python script in R using the Anaconda python, not the system one:</p>

<p><code>system("/home/user/anaconda3/bin/python3 code.py",intern=TRUE)</code></p>

<p>Alternatively, you could add <code>/home/user/anaconda3/bin/</code> to your <code>PATH</code> environment variable in <code>~/.bashrc</code> so that it chooses anaconda over the system binary.</p>

python bash shell