0

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.

mabe
  • 125
  • 1
  • 10
  • 1
    Did you try to set the engine path? `knitr::opts_chunk$set(engine.path = list(python = '~/anaconda/bin/python'))` – Martin Schmelzer Dec 11 '20 at 12:46
  • Thank you for the hint. I added the line ```knitr::opts_chunk$set(engine.path = list(python = '~/anaconda3/envs/gwpy37/bin/python3'))``` but get the same result as described above. – mabe Dec 11 '20 at 13:55
  • Maybe a python 3.7 problem? Code finally knitted to html with python 3.8. I used the opts_chunk$set suggested by Martin Schmelzer. – mabe Dec 12 '20 at 15:59
  • I'm closing this. I was not able to get the markdown to compile to html with any conda environment, only with brew installations. Ended up not working with conda but specifying my brew installed python3 with ```reticulate::use_python``` and ```knitr::opts_chunk$set(engine.path...```. This works fine. – mabe Dec 12 '20 at 20:05

0 Answers0