madness madness - 11 months ago 52
Python Question

Python script works in bash, but not when called from R via system

I use Ubuntu 16.04. I'm trying to run a simple python script from R. The script is

import numpy as np

and is written in a file named It works fine if I call it in bash via


However, when I call it in R via


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



If I run a file with

import sys

I get

[1] "['/home/user/Desktop', '/usr/lib/', '/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']"

if I run the file from R, and

['/home/user/Desktop', '/home/user/anaconda3/lib/', '/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']

if I run the file from the command line.

Answer Source

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).

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).

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


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