Problem using reticulate with sourcing python script when Markdown knitting on mac os

Hi,
New to both knitting, reticulate, and python.
I am trying to html knit a "hello world" code chunk with reticulate R, but without success.
The (green) "Run Current Chunk" button works fine within the .Rmd project.
Yet, the knitting process halts on my reticulate::source_python() call.
The R Markdown console output tab halts stating "label: unnamed-chunk-1" (at 67%).

Using mac os catalina (10.15.7), anaconda for mac (Navigator 1.9.12), and having the latest rstudio/reticulate installed via remotes::install_github("rstudio/reticulate"). I am not using miniconda, only using the base default environment in anaconda.

Below is the code in .Rmd file called test.Rmd;

---
title: "R Notebook"
output:
  html_document: default
---

```{r}
library('reticulate')
library("knitr")
use_python("/Users/pept/opt/anaconda3/bin/python")
use_condaenv('base', conda = "/Users/pept/opt/anaconda3/bin/conda")
#use_virtualenv("r-reticulate")
#reticulate::py_config()
#reticulate::py_discover_config()
reticulate::source_python("~/Desktop/test.py")
#reticulate::source_python("test.py")
#print(py$add(2,3))

Below is the code is the Python "hello world" source script called test.py;

def add(x,y):
 return x + y

Any help or assistance would be very much appreciated.
Thanks.

Why not use the reticulate python engine ?

You would be able to use you python code directly in a python chunk.

did you try already ?

Dear cderv,
Thank You so much for Your reply.
I think I tried that option.
Eventually I settled for adding the (hidden) file ".Renviron" where the .Rmd file resides;
and setting the RETICULATE_PYTHON variable by the line;
Sys.setenv(RETICULATE_PYTHON = "/Users/pept/opt/anaconda3/bin/python3")

Now below code (at least )knits to html;

title: "R Notebook"
output:
  html_document: default
library('reticulate')
library("knitr")
knitr::knit_engines$set(python = reticulate::eng_python)
#reticulate::use_python("/Users/pept/opt/anaconda3/bin/python3")
reticulate::use_condaenv('base', conda = "/Users/pept/opt/anaconda3/bin/conda")
reticulate::py_config()
reticulate::py_discover_config()
reticulate::source_python("test.py")
print(py$add(2,3))

Yet, not sure if I am pointing to the anaconda python assignment in the ".Renviron" as both the py_config() and py_discover_config() seems to point to the "default installed" mac python path /usr/bin/python (with python 2.7).

Knitting seems to succeed even with
knitr::knit_engines$set(python = reticulate::eng_python)
commented out.

Best Regards,
Per

This topic was automatically closed 21 days after the last reply. New replies are no longer allowed.

If you have a query related to it or one of the replies, start a new topic and refer back with a link.