Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Open
Obihoernchen80 opened this issue Aug 1, 2024 · 7 comments
Open

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

Obihoernchen80 opened this issue Aug 1, 2024 · 7 comments

Comments

@Obihoernchen80
Copy link

Obihoernchen80 commented Aug 1, 2024

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:

rpy2/rpy2#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
Copy link
Member

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
Copy link
Author

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
Copy link
Member

dfalbel commented Aug 1, 2024

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
Copy link
Member

@t-kalinowski
Copy link
Member

t-kalinowski commented Aug 1, 2024

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
Copy link
Member

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

@jall-in
Copy link

jall-in commented Sep 16, 2024

Hi @lgautier / @t-kalinowski: is there any progress on this topic or any workaround? I'm running into the exact the same issue. Many thanks

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants