I have the following example markdown where I load an environment en with python 3.7, numpy 1.19.2 and dependencies:
title: "test knitr python"
output: html_document
---
```{r setup, include=FALSE}
library(reticulate)
use_condaenv('en') # Tell R which python environment to use.
knitr::opts_chunk$set(echo = TRUE)
```
```{python}
a = 1
```
```{r}
py$a
```
which outputs the expected [1] 1
when I Run All in RStudio. However, when I try to knit the code to html, the output in the R Markdown console runs up to the python chunk and then stalls there without error message and no html is produced. This is the last line in the R Markdown console: $ engine: chr "python"
.
I use the following software:
- Mac OS 10.15
- RStudio 1.3
- knitr 1.30
- reticulate 1.18
- python 3.7.9
- numpy 1.19.2
reticulate::py_config
shows the expected environment.
Here's what I tried so far:
- Searching the reticulate and knitr documentation for examples specific to python3 (found none so I assume it should in principle be straight forward)
- Searched for similar questions on stack overflow. I followed up on all of the following Q&As and comments and tried out suggested solutions but either wasn't able to figure out how the answer could solve my problem (e.g. here) or I wasn't able to get my minimal example to work properly with the suggested answers (e.g. here, adding
use_python
in the setup chunk or specifying engine.path in the python chunk). All resulted in the same behaviour in the R Markdown console. - Tried to run the code with the default python reticulate would choose if I do not specify an environment by restarting the R session and commenting out the
use_conda
line.reticulate::py_config
uses python 2.7 and trows the following error when reaching the python chunk:
Error in py_call_impl(callable, dots$args, dots$keywords) :
AttributeError: 'module' object has no attribute '__import__'
Detailed traceback:
File "/Library/Frameworks/R.framework/Versions/4.0/Resources/library/reticulate/python/rpytools/loader.py", line 13, in initialize
_import = builtins.__import__
There seems to be a problem when loading python. I cannot explain this error message but it may be related with why I cannot get the markdown example above to run.
I'd very much appreciate some hints about how I can proceed to get this code running.