Reloading Python modules with R reticulate package

时间:2018-08-22 13:51:11

标签: r reticulate

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.

1 个答案:

答案 0 :(得分:0)

可以通过调用Python命令重新加载:

对于Python 2.7

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

对于Python 3

> importlib <- import("importlib")
> importlib$reload(your_module)