0

I am trying to run an R script from the command line using Rscript. The R script calls some python code and works fine when run interactively. However, when I call it from Rscript I get some errors with gdal. I believe the errors are related to python 3 vs 2. For example, running the following command in an interactive R session:

system("python --version)

yields

Python 2.7.9

while running the "same" command from the shell:

$ python --version

yields

Python 3.5.1 :: Continuum Analytics, Inc.

How can I point my $ Rscript "foo.R" calls to python 2.7.9?

jsta
  • 3,216
  • 25
  • 35

2 Answers2

0

When you invoke either $ python --version or $ Rscript -e 'system("python --version")' from the shell command line, you are operating in a Python virtual environment, perhaps one created by virtualenv. Specifically, you are executing /home/foo/miniconda3/bin/python

When you run system("python --version") from RStudio, you get the system default version. Specifically, you are executing /usr/bin/python.

How can I point my $ Rscript "foo.R" calls to python 2.7.9?

That depends upon how you entered your virtual Python environment. If it was one created by virtualenv, simply deactivate it:

$ deactivate
$ Rscript -e 'system("python --version")'
2.7.9

EDIT: You appear to be using virtual environments created by miniconda. If that is the case, you can use the system default Python by editing your PATH environment variable. How you edit your PATH depends upon how long you want your change to have effect.

  • For your change to lost for only a single command, try this:

    $ PATH="$(echo "$PATH" | sed -e s,:/usr/local/sbin,,)" Rscript -e 'system("python --version")' 
    
  • For your change to last the duration of a single shell session, try this:

    $ PATH="$(echo "$PATH" | sed -e s,:/usr/local/sbin,,)"
    $ python --version
    $ Rscript -e 'system("python --version")' 
    
  • To permanently change your PATH, edit the file $HOME/.bashrc appropriately. You will need to start a new terminal session for this to have affect.

Robᵩ
  • 163,533
  • 20
  • 239
  • 308
  • You are such an excellent editor, if you know what I mean? ;) – Dataman Apr 07 '16 at 16:48
  • I understand that the paths are different. However, I have no idea what `virtualenv` is. `$ virtualenv` > The program 'virtualenv' is currently not installed. You can install it by typing: sudo apt-get install virtualenv – jsta Apr 07 '16 at 17:13
  • @jsta -- That means that you didn't use virtualenv to create your virtual python environment. How did you create /home/foo/miniconda3, and how did you cause it to be first on your PATH? However you did that, that is what you have to undo. – Robᵩ Apr 07 '16 at 18:23
0

The solution was to insert a call to the methods library in my R script and create/load a python 2 environment using the command:

$ conda create --name python2 python=2
$ source activate python2

See:
Rscript does not load methods package, R does -- why, and what are the consequences?
and
http://conda.pydata.org/docs/py2or3.html

Community
  • 1
  • 1
jsta
  • 3,216
  • 25
  • 35