How to import local Python package into R via reticulate

Viewed 19

I am encountering an issue when importing Python scripts - which reference a local Python package in a subfolder - into R. The Python code is properly structured with __init__.py files, and executes as expected when called in Python, finding the local package in its imports.

e.g. I have this Python file reticulate_test3.py, which references an internal package, located in an "operations" subfolder:

from operations.local.io import get_file_name


def reticulate_test3():
  print('Calling Python with a nested package from R!')


if __name__ == "__main__":
  procedure_name = get_file_name(__file__)
  print(f"Running {procedure_name} procedure")
  reticulate_test3()
  print(f"Execution of {procedure_name} finished")

But if I call it from R, then it cannot find the local package.

library(reticulate)

call_print <- function() {
  print("hi")
}

...

call_reticulate_test3 <- function() {
  # Not the actual path
  py_run_file("C:\\dev...python\\reticulate_test3.py")
}

When calling call_reticulate_test3(), I get this error when it parses the file:

! callr subprocess failed: ModuleNotFoundError: No module named 'operations'

How can I load the entirety of the local package properly using Reticulate so it knows how to locate modules within the package?

0 Answers
Related