Skip to content

Problem to run python code in r markdown

4 messages · Kai Yang, Eric Berger, Milan Glacier +1 more

#
Hi Team,I'm trying to run python in R markdown (flexdashboard). The code is below:

try Python=============
```{r, include=FALSE, echo=TRUE}library(reticulate)py_install("numpy")
use_condaenv("base")
```
```{python}import numpy as np```

I got error message below:
Error in py_call_impl(callable, dots$args, dots$keywords) :?? ModuleNotFoundError: No module named 'numpy'Calls: <Anonymous> ... py_capture_output -> force -> <Anonymous> -> py_call_implIn addition: There were 26 warnings (use warnings() to see them)Execution halted


Based on message, the python can not find numpy package. But I'm sure I installed the package. I don't know how to fix the problem. please help
Thank you,Kai
#
You don't specify what platform you are on (linux / Windows / other),
and what tools you are using.
I am running a linux system and I have virtual environments set up.
I set the environment variable RETICULATE_PYTHON to point to python
(in my virtual environment).
I am using RStudio, and I use the File pulldown menu and select New
File --> 'R Markdown' or New File --> 'Quarto document'.
In the R Markdown (or Quarto) document, create a python code chunk:

```{python}
import numpy as np
print(np.arange(5))
```

works for me.
HTH,
Eric




On Fri, Jan 20, 2023 at 6:20 PM Kai Yang via R-help
<r-help at r-project.org> wrote:
#
Maybe it is the order. You called py_install() first and then called
use_condaenv(). Perhaps you need to switch to your conda env first and
then call py_install().
On 01/20/23 16:20, Kai Yang via R-help wrote:
#
Kai,

Just FYI, this is mainly an R mailing list and although there ware ways to combine python with R (or sort of alone) within environments like RSTUDIO, this may not be an optimal place to discuss this. You are discussing what is no longer really "R markdown" and more just plain "markdown" that supports many languages in many environments and you are using one called flexdashboard.

I had a similar problem for a while because I had an amazing number of copies of python installed on my machine in various ways including directly and within Anaconda and several other ways. Each may have kept copies of modules like numpy in a different place. Unfortunately, when running python from RSTUDIO, it was looking in a place that did not have it and when I used "pip" to get the latest copy, it insisted I already had it!

So I eventually uninstalled all versions I could find and reinstalled just one version and then got the numpy and pandas modules installed under that version. Oh, I also replaced/updated RSTUDIO. Things work fine for now.

Some people may advise you on how to determine which version of python you are calling, or change it, and how to download numpy to the place it is expected, or change some environmental variable to point to it or any number of other solutions. Some like virtual environments, for example.

The bottom line is your setup does not have numpy installed as far as the software is concerned even if it is installed somewhere on your machine. When you get things aligned, you will be fine. 

-----Original Message-----
From: R-help <r-help-bounces at r-project.org> On Behalf Of Kai Yang via R-help
Sent: Friday, January 20, 2023 11:20 AM
To: R-help Mailing List <r-help at r-project.org>
Subject: [R] Problem to run python code in r markdown

Hi Team,I'm trying to run python in R markdown (flexdashboard). The code is below:

try Python=============
```{r, include=FALSE, echo=TRUE}library(reticulate)py_install("numpy")
use_condaenv("base")
```
```{python}import numpy as np```

I got error message below:
Error in py_call_impl(callable, dots$args, dots$keywords) :   ModuleNotFoundError: No module named 'numpy'Calls: <Anonymous> ... py_capture_output -> force -> <Anonymous> -> py_call_implIn addition: There were 26 warnings (use warnings() to see them)Execution halted


Based on message, the python can not find numpy package. But I'm sure I installed the package. I don't know how to fix the problem. please help Thank you,Kai
















______________________________________________
R-help at r-project.org mailing list -- To UNSUBSCRIBE and more, see https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
and provide commented, minimal, self-contained, reproducible code.