Reloading Python modules with R reticulate package
Asked Answered
P

1

9

I would like to know if there is a way to reload modules imported into R via the reticulate package, similar to the autoreload extension for IPython. For example, suppose I am developing my own module locally, I might import it via

library(reticulate)
import_from_path("mypackage", "/path/to/package")

However, if I make code changes to mypackage the only way I can think to reflect them is to restart R. This is consistent with normal Python behavior where modules cannot be reloaded. One workaround would be to source a file from the package in which I am making changes, i.e.

source_python("/path/to/package/file.py")

However, if file.py is importing other files in the local package that are being changed than changes to those files will not be reflected.

Pandemic answered 22/8, 2018 at 13:51 Comment(3)
If you use devtools you can use load_all() to load your package in and reload when changes are made.Prefatory
My understanding is that load_all is just for R packages, whereas here I am importing a Python package into R.Pandemic
I think, this would be really nice feature!Astoria
R
3

It's possible by calling Python commands to reload:

For Python 2.7

> builtins <- import_builtins()
> builtins$reload(your_module)

For Python 3

> importlib <- import("importlib")
> importlib$reload(your_module)
Runagate answered 22/11, 2019 at 19:12 Comment(0)

© 2022 - 2025 — McMap. All rights reserved.