COLO COLO - 1 month ago 8
R Question

How can I specify rmarkdown to use python3 instead python 2?

I am trying to run python as rmarkdwon code chunks. I was sucessfull 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 pytthon 3.5, which is the one I want rmarkdown use.
Here is the code and output of the python chunk in rmarkdown

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


and the output:

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


Any ideas?

Answer Source

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