10

I am trying to run python as rmarkdwon code chunks. I was successful but rmarkdown by default uses Python2 and I want it to use Python 3. I am running it on Ubuntu with Python 2.7.6 installed and I installed anaconda with Python 3.5, which is the one I want rmarkdown use.

Here is the code and output of the python chunk in rmarkdown

import sys
print (sys.version)

and the output:

2.7.6 (default, Jun 22 2015, 17:58:13) 

Any ideas?

marc_s
  • 732,580
  • 175
  • 1,330
  • 1,459
COLO
  • 1,024
  • 16
  • 28
  • http://stackoverflow.com/questions/13336852/setting-python3-2-as-default-instead-of-python2-7-on-mac-osx-lion-10-7-5 – ytk Aug 21 '16 at 21:59

4 Answers4

15

You can add engine.path = '/path/to/python3' to override the python (2) executable. For example,

---
title: "python"
output: html_document
---

```{r setup, include=FALSE}
knitr::opts_chunk$set(echo = TRUE)
```

```{python}
import sys
print(sys.version)
```

```{python, engine.path = '/usr/bin/python3'}
import sys
print(sys.version)
```

enter image description here

nrussell
  • 18,382
  • 4
  • 47
  • 60
  • I have installed python 3.5 in the directory: 'code' //home/user1/anaconda3/bin 'code', so I just used 'code' ```{python, engine.path = '~/user1/anaconda3/bin/python3'} 'code' and I get the following error: 'code'Error in system2(cmd, code, stdout = TRUE, stderr = TRUE, env = options$engine.env) : error in running command Calls: ... tryCatch -> tryCatchList -> tryCatchOne -> Execution halted 'code' – COLO Aug 22 '16 at 13:27
  • Open up a bash terminal and type in `which python3`. Whatever that returns is what you should use for `engine.path`. – nrussell Aug 22 '16 at 13:29
  • What is the **exact** path returned by `which python3`? – nrussell Aug 22 '16 at 13:32
  • which pyhton3 returned /home/user1/anaconda3/bin/python3 so I use this ```{python, engine.path = '~/home/user1/anaconda3/bin/python3'}. Still have the same error. Also tried ```{python, engine.path = '~/user1/anaconda3/bin/python3'} whit no success. – COLO Aug 22 '16 at 13:34
  • 1
    Try using *exactly* this path: `/home/user1/anaconda3/bin/python3`. Currently you are entering `~/home/user1/anaconda3/bin/python3`, which is presumably expanding to `/home/user1/home/user1/anaconda3/bin/python`. – nrussell Aug 22 '16 at 13:38
5

You can select your desired python version, as default, with an R chunk:

```{r setup, echo=FALSE}
library(knitr)
opts_chunk$set(engine.path = '/usr/bin/python3')
```

From now on your python chunks will use Python3:

```{python}
import sys
print(sys.version)
```

This way to select python version avoids to add the engine.path variable to every code chunk.

Enrique Pérez Herrero
  • 3,699
  • 2
  • 32
  • 33
  • 1
    Neither setting my `opts_chunk` like this nor editing my .Rprofile to add `Sys.setenv(PATH = paste("", Sys.getenv("PATH"), sep=":"))` has set the default python correctly. I have no trouble setting an individual chunk to run python3 by setting the `engine.path` for that chunk, but setting all chunks to default to python3 hasn't worked; it still defaults to python2. I'm on version 1.1.423 of RStudio for Mac and R version 3.3.3. Would love to hear any suggestions! – amanda Feb 19 '18 at 13:34
0

Actually, if you are running Python 3.x, which I am, I had to do the following (obviously your path to the Python3 executable might be different):

```{r Setup, echo=FALSE}
library(knitr)
opts_chunk$set('python', engine.path='/usr/local/Cellar/python3/3.6.3/bin/python3')

```

And could then create a chunk using just python and it would run in the correct version:

```{python, cache=TRUE, echo = FALSE, eval = TRUE}

import sys
print(sys.version)

```
Shanemeister
  • 667
  • 7
  • 13
0

You can use the reticulate package and insert an R chunk at the beginning:

```{r}
library(reticulate)
use_python("/usr/local/bin/python3")
```
00schneider
  • 698
  • 9
  • 21