madness
madness

Reputation: 381

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
    x=1
    print(x)

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

    python3.5 code.py

However, when I call it in R via

    system("python3.5 code.py",intern=TRUE)

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

Thanks!

UPDATE

If I run a file with

     import sys 
     print(sys.path)

I get

     [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']" 

if I run the file from R, and

    ['/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']

if I run the file from the command line.

Upvotes: 1

Views: 307

Answers (1)

Gillespie
Gillespie

Reputation: 6561

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:

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

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

Upvotes: 1

Related Questions