title: How can I get knitr to output python 3 code and make it available in R chunks?
body: <p>I have the following example markdown where I load an environment <em>en</em> with python 3.7, numpy 1.19.2 and dependencies:</p>
  <pre><code>    title: &quot;test knitr python&quot;
      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
      ```
  </code></pre>
  <p>which outputs the expected <code>[1] 1</code> when I <em>Run All</em> 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: <code>$ engine: chr &quot;python&quot;</code>.</p>
  <p>I use the following software:</p>
  <ul>
  <li>Mac OS 10.15</li>
  <li>RStudio 1.3</li>
  <li>knitr 1.30</li>
  <li>reticulate 1.18</li>
  <li>python 3.7.9</li>
  <li>numpy 1.19.2</li>
  </ul>
  <p><code>reticulate::py_config</code> shows the expected environment.</p>
  <p>Here's what I tried so far:</p>
  <ul>
  <li>Searching the <a href="https://rstudio.github.io/reticulate/index.html" rel="nofollow noreferrer">reticulate</a> and <a href="https://yihui.org/knitr/" rel="nofollow noreferrer">knitr</a> documentation for examples specific to python3 (found none so I assume it should in principle be straight forward)</li>
  <li>Searched for similar questions on stack overflow. I followed up on all of the following Q&amp;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. <a href="https://stackoverflow.com/questions/57895993/knitting-in-r-markdown-file-stops-without-printing-error-when-hitting-a-python-c">here</a>) or I wasn't able to get my minimal example to work properly with the suggested answers (e.g. <a href="https://stackoverflow.com/questions/39069158/how-can-i-specify-rmarkdown-to-use-python3-instead-python-2">here</a>, adding <code>use_python</code> in the setup chunk or specifying engine.path in the python chunk). All resulted in the same behaviour in the R Markdown console.</li>
  <li>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 <code>use_conda</code> line. <code>reticulate::py_config</code> uses python 2.7 and trows the following error when reaching the python chunk:</li>
  </ul>
  <pre><code>Error in py_call_impl(callable, dots$args, dots$keywords) : 
    AttributeError: 'module' object has no attribute '__import__'
  
  Detailed traceback: 
    File &quot;/Library/Frameworks/R.framework/Versions/4.0/Resources/library/reticulate/python/rpytools/loader.py&quot;, line 13, in initialize
      _import = builtins.__import__
  </code></pre>
  <p>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.</p>
  <p>I'd very much appreciate some hints about how I can proceed to get this code running.</p>