Yehoshaphat Schellekens Yehoshaphat Schellekens - 1 year ago 44
Bash Question

system commant wont work as in shell when calling a python comman on Ubuntu from R

I'm trying to invoke within

a system command that invokes a call to a python script (that includes an import to pandas) as following:

[1] "/home/production"
> system("python In_tag_main_model/python_scripts/")
Traceback (most recent call last):
File "In_tag_main_model/python_scripts/", line 11, in <module>
import pandas as pd
ImportError: No module named pandas

i'm calling pandas, which isn't recognized, though when i'm running this exact command from my machines shell:

production@va-rsrv01:~$ python In_tag_main_model/python_scripts/

It works great,any ides why
isn't working for me?

Thanks in advance!

Answer Source

It looks like the R system function is executing a different python executable. You have three options to specify which executable you want:

  • You the absolute path:

    system("/anaconda2/bin/python In_tag_main_model/python_scripts/")
  • Set the PATH variable for your process via Sys.setenv (as you have done):

    system("python In_tag_main_model/python_scripts/") 
  • Use the newer system2 function which provides an env argument that can be used to modify the environmental variables for the subprocess:


    Note that system2 has a different calling convention than system.