Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why do which and Sys.which return different paths?

Tags:

macos

r

rstudio

I tried to run a Python script from R with:

system('python script.py arg1 arg2')

And got an error:

ImportError: No module named pandas

This was a bit of a surprise since the script was working from the terminal as expected. Having encountered this type of issue before (with knitr, whence the engine.path chunk option), I know to check:

Sys.which('python')
#            python 
# "/usr/bin/python"

And compare it to the command line:

$ which python
# /Users/michael.chirico/anaconda2/bin/python

(i.e., the error arises because I have pandas installed for the anaconda distribution, though TBH I don't know why I have a different distribution)

Hence I can fix my issue by running:

system('/Users/michael.chirico/anaconda2/bin/python script.py arg1 arg2')

My question is two-fold:

  • How does R's system/Sys.which find a different python than my terminal?
  • How can I fix this besides writing out the full binary path each time?

I read ?Sys.which for some hints, but to no avail. In particular, ?Sys.which suggests Sys.which is using which:

This is an interface to the system command which

This is clearly (?) untrue; to be sure, I checked Sys.which('which') and which which to confirm both are pointing to /usr/bin/which (goaded on by this tidbit):

On a Unix-alike the full path to which (usually /usr/bin/which) is found when R is installed.

To the latter, on a whim I tried Sys.setenv(python = '/Users/michael.chirico/anaconda2/bin/python') to no avail.

like image 667
MichaelChirico Avatar asked Oct 29 '22 21:10

MichaelChirico


1 Answers

As some of the comments hint, this is a problem that arises because the PATH environment variable is different for programs launched by Finder (or the Dock) than it is in the Terminal. There are ways to set the PATH for Dock-launched applications, but they aren't pretty. Here's a place to start looking if you want to go that route:

https://apple.stackexchange.com/questions/51677/how-to-set-path-for-finder-launched-applications

The other thing you can do, which is probably more straightforward, is tell R to set the PATH variable when it starts up, using Sys.setenv to add the path to your desired Python instance. You can do that for just one project, for your whole user account, or for the whole system, by placing the command in a .Rprofile file in the corresponding location. More information on how to do this here:

https://stat.ethz.ch/R-manual/R-devel/library/base/html/Startup.html

like image 94
Jonathan Avatar answered Nov 10 '22 17:11

Jonathan