1

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.

4
  • 3
    Add this to your program and see if it produces different output depending on the context: import sys print(sys.path) Commented Sep 6, 2016 at 19:44
  • It says Traceback (most recent call last): File "test.py", line 4, in <module> import numpy as np ImportError: No module named 'numpy' [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']" attr(,"status") [1] 1 Warning message: running command 'python3.5 test.py' had status 1 Commented Sep 8, 2016 at 13:12
  • Make a new python file with just those two lines. Run in both R and from the command line and compare the outputs. Commented Sep 8, 2016 at 14:27
  • I've updated my question with this info, otherwise it wouldn't fit here Commented Sep 8, 2016 at 15:13

1 Answer 1

1

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.

Sign up to request clarification or add additional context in comments.

1 Comment

Thanks! I've used system("/home/user/anaconda3/bin/python3 code.py",intern=TRUE) and it works!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.