-
Notifications
You must be signed in to change notification settings - Fork 330
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
import inside py_capture_output breaks delay_load #1565
Comments
What is the output you are trying to suppress? ( |
@t-kalinowski This is a minimum working example, I am not importing |
We don't currently have an official API for this. Thinking though how this could be implemented, an R package could probably do something like this (completely untested code): .onLoad <- function(...) {
if (reticulate::py_available())
reticulate::py_capture_output({
module <<- reticulate::import("module")
})
else {
py_output_context_manager <- NULL
module <<- import("module", delay_load = list(
before_load = function() {
reticulate::py_available(TRUE) # initialize python
# get output tools helper functions
output_tools <- import("rpytools.output")
py_output_context_manager <<-
output_tools$OutputCaptureContext(capture_stdout = TRUE,
capture_stderr = TRUE)
py_output_context_manager$`__enter__`()
}
on_load = function() {
py_output_context_manager$`__exit__`()
py_output_context_manager <<- NULL
},
on_error = function(error) {
py_output_context_manager$`__exit__`()
py_colected_output <- py_output_context_manager$collect_output()
py_output_context_manager <<- NULL
error$message <- paste0(c(error$message, py_colected_output), sep = "\n")
stop(error)
}
))
}
} However, taking a look at 'pykeops' specifically, seems like this is all that's needed to suppress output: Sys.setenv("PYKEOPS_VERBOSE" = "0") |
Thanks a lot @t-kalinowski! I'll work on that. Regarding the environment variable setup, I want to catch the output from Python and print it with |
Worth mentioning if you're going to be setting env variables: if Python is initialized already, changes to environment variables made with So you'll want to do something like this: Sys.setenv("PYKEOPS_VERBOSE" = "0")
if(py_available())
import("os")$environ$update(list("PYKEOPS_VERBOSE" = "0")) I might be mistaken, but I don't think With I just pushed a small updated to |
@t-kalinowski thank you very much again. You are right regarding |
I am using
reticulate::import()
withdelay_load=TRUE
to load a Python package in the.onLoad()
function of my R package (so that user can setup their Python session, e.g. using a virtual environment, afterward).Said Python package is printing to stdout when being imported, which I want to avoid because it is good practice that R package verbosity on loading could be disabled by the user (for quiet loading).
So I tried to wrap my
reticulate::import()
call inside areticulate::py_capture_output()
call, however the delay loading is not working anymore. Below is a minimum working example:SciPy
is not available, direct import fails as expected:SciPy
is not available yet but import with delay loading works as expected (e.g. I may callreticulate::use_virtualenv()
to setup a Python virtual environment whereSciPy
is installed afterward):py_capture_output()
call does not work:Note: this behavior is maybe expected since
reticulate
may need to initiate the Python session to callpy_capture_output()
. In that case, how could I avoid verbosity printing to stdout when importing a Python package during my R package loading ?Thanks
Edit1: this is a minimum working example,
SciPy
import produces no output that I would need to capture. My R package is importing another Python package that produces output at import, namelypykeops
.Edit2: this is my R session info:
The text was updated successfully, but these errors were encountered: