rstudio / reticulate

R Interface to Python
https://rstudio.github.io/reticulate
Apache License 2.0
1.67k stars 327 forks source link

can't use rpy2 inside reticulate-embedded python #1638

Open Obihoernchen80 opened 1 month ago

Obihoernchen80 commented 1 month ago

Describe the issue or bug and how to reproduce

This works in python console:

>>> import rpy2.robjects as robjects

This fails in R console:

reticulate::import("rpy2.robjects")

The following message is infinitely printed ****found a symbol with attributes

Same issue, reported on rpy2 github:

https://github.com/rpy2/rpy2/issues/942

Package versions

windows 10 64bit R core : 4.3.0 reticulate: 1.38.0 rpy2: 3.5.16 python: 3.10.11

t-kalinowski commented 1 month ago

Hi, thanks for reporting. I tried but can't reproduce locally. The import("rpy2.robjects") command succeeds without error for me:

> reticulate::import("rpy2.robjects")
/Users/tomasz/.virtualenvs/r-reticulate/lib/python3.10/site-packages/rpy2/rinterface_lib/embedded.py:276: UserWarning: R was initialized outside of rpy2 (R_NilValue != NULL). Trying to use it nevertheless.
  warnings.warn(msg)
R was initialized outside of rpy2 (R_NilValue != NULL). Trying to use it nevertheless.
Module(rpy2.robjects)

Do you perhaps have any custom Makevars setup?

Obihoernchen80 commented 1 month ago

Do you perhaps have any custom Makevars setup?

No.

From what I've gathered it seems to be windows issue and from your paths, I'd say you are running Linux?

dfalbel commented 1 month ago

I can also reproduce this on Windows. Here's what's on the stack after plugging WinDbg

This is all running threads:

Screenshot 2024-08-01 101959

This is the R main thread: Screenshot 2024-08-01 102047

This is the other thread where reticulate appears: Screenshot 2024-08-01 102126

t-kalinowski commented 1 month ago

It seems the error is coming from here: https://github.com/wch/r-source/blob/b12ffba7584825d6b11bba8b7dbad084a74c1c20/src/main/memory.c#L1742

t-kalinowski commented 1 month ago

I'm not a Windows machine presently, but reading the source code, it seems that rpy2 is trying to initialize R when R is already initialized.

It seems that Rf_initEmbeddedR() is called by rpy2, but only on Windows, from _initr_win32() which is set on module import: https://github.com/rpy2/rpy2/blob/ae74c26deca603786acfe0bdb03433bfc74975fc/rpy2/rinterface.py#L41-L43

That, calls openrlib.rlib.Rf_initEmbeddedR https://github.com/rpy2/rpy2/blob/ae74c26deca603786acfe0bdb03433bfc74975fc/rpy2/rinterface_lib/embedded_mswin.py#L48

Which leads to the error where the R symbol table is being attempted to be initialized twice. The stacktrace @dfalbel posted confirms that we have an Rf_initEmbeddedR call in the stack, after the R main loop is already running.

It's not clear to me why rpy2 is attempting to initialize R. The other output from rpy2 indicates that some parts of rpy2 are correctly detecting that R is already initialized. A Python stacktrace from the _initr_win32() call would be the next step.

t-kalinowski commented 1 month ago

@lgautier Do you know why rpy2 might be attempting to initialize R when running embedded under reticulate on Windows?