How to source a python file that imports another python file using reticulate in an R-package

I have an R-package with 2 python files in inst/python called and is sourced with reticulate, where in turn needs to import But fails to find is in R sourced like this:

                                        package = "My_Package_Name",
                                        mustWork = TRUE
  )) is in turn trying to import, like this:

import os, sys
from B import main

When running the function that uses and B.y this is the resulting error:
Error in py_run_file_impl(file, local, convert) : **
** ModuleNotFoundError: No module named 'B'

I have tried to source before sourcing in R using reticulate::source_python().
I have also tried in to find the correct path to the That is, in R I print the current position for where R finds the file, which is where the R-package files are located in the system:
However, the exact location of this will of course differ across OS etc.

When sourcing in R, that file in turn looks for but in the wrong location.
This is what I’ve tried in python (followed by the printed message):







print( os.path.abspath(sys.argv[0]))

Any help is much appreciated.

i got this to work by setting the python path from R using reticulate like this:

  My_Package_Name_path <- system.file("python", package = "My_Package_Name")
  reticulate::py_run_string(paste0("import sys; sys.path.append('", My_Package_Name_path, "')"))

                                        package = "My_Package_Name",
                                        mustWork = TRUE

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.

If you have a query related to it or one of the replies, start a new topic and refer back with a link.