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?

1

4 Answers 4

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

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

5 Comments

I have installed python 3.5 in the directory: 'code' /<root>/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: <Anonymous> ... tryCatch -> tryCatchList -> tryCatchOne -> <Anonymous> Execution halted 'code'
Open up a bash terminal and type in which python3. Whatever that returns is what you should use for engine.path.
What is the exact path returned by which python3?
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.
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.
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.

1 Comment

Neither setting my opts_chunk like this nor editing my .Rprofile to add Sys.setenv(PATH = paste("<path_to_python3>", 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!
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)

```

Comments

0

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

```{r}
library(reticulate)
use_python("/usr/local/bin/python3")
```

Comments

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.