Importing python packages in RStudio using reticulate

I'm using a Mac and have an Rstudio project ( my_project below) which I set up with renv and in my .Rprofile inside the project's directory I put these lines:

source("renv/activate.R")
renv::use_python()

Then in RStudio I did Session -> Restart R , which created R and Python virtual environments.

Then in RStudio 's terminal window I ran the command:

renv/python/virtualenvs/renv-python-3.7.3/bin/pip install pandas

I then installed the reticulate R package.

Now, within RStudio I'm trying to import the pandas python package using:

renv::use_python()
library(reticulate)
pd <- reticulate::import("pandas")

And after the third line I'm getting:

Error: Python shared library not found, Python bindings not loaded.
Use reticulate::install_miniconda() if you'd like to install a Miniconda Python environment.

I also tried sourcing the python virtualenv in RStudio 's terminal window:

dr-mba-1035:scRNAseqShiny dr$ source /Users/dr/my_project/renv/python/virtualenvs/renv-python-3.7.3/bin/activate
(renv-python-3.7.3) dr-mba-1035:my_project dr$ 

And then go and repeat the R code lines above but I'm getting the same error.

However, I am able to launch python from that command line window and successfully import pandas there.

Any idea?

Can you confirm what version of Python is being loaded by reticulate? What is the output of:

reticulate::py_config()

In addition, do you know how this copy of Python was built? If it wasn't build with a shared library, that would also explain the associated error here:

Error: Python shared library not found, Python bindings not loaded.

Note that Python needs to be configured with

./configure --enable-shared

during installation, so that a Python shared library is generated that reticulate can load.

Thanks a lot for the response.

Per the first question, I'm getting the same error as before:

> reticulate::py_config()
Error: Python shared library not found, Python bindings not loaded.
Use reticulate::install_miniconda() if you'd like to install a Miniconda Python environment.

Per the second question, I imagine it wasn't built with a shared library. What I did, which are the steps I described in my post above, is to be able to publish this code (which is a shiny app) on RStudio Connect, which works. Right now, I'm trying to do some debugging of this code on my Mac (where I'm publishing from) and I'm getting this RStudio/reticulate specific error.

Per the configuration with:
./configure --enable-shared

During installation of what should I use this configuration? (sorry if my question is banal, I'm not well versed in Python)

The simplest fix might be to just install Miniconda, and use that instead -- try running

reticulate::install_miniconda()

and then reticulate should select an appropriate environment with that Miniconda installation, which should work out of the box.

I installed Miniconda with:
reticulate::install_miniconda()

Which installed it to:
/Users/dr/Library/r-miniconda

But repeating:

renv::use_python()
library(reticulate)
pd <- reticulate::import("pandas")

Still gives the same error:

Error: Python shared library not found, Python bindings not loaded.
Use reticulate::install_miniconda() if you'd like to install a Miniconda Python environment.

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.