Search code examples
pythonrreticulate

Python env in R: Installing python packages in R


I want to read a python file in R and I am using reticulate package for that.

I have certain packages in python file. Do I have to install them in R env again somehow, for the file to run? And how do I do that? I am getting this error:

  Error in py_run_file_impl(file, local, convert) : 
  ModuleNotFoundError: No module named 'descartes'

How do I fix this?


Solution

  • What worked for me was first installing the python customLibrary via the python command line interface.

    Second, in my R project folder I specified the python function in a file customLibrary.py which specifies import as first line, such as:

    import customLibrary as CL
    def custoFunction(path):
       A = CL.test(path)
       return(A)
    

    Third, I call the function from R via standard reticulate::source_python("customLibrary.py") implementation (You may need to check whether your directory-path is correct via R command getwd().)

    It may be helpful to start with small steps to narrow down the issue:

    • just write a simple function, such as "addThree" that is essentially y = x + 3 etc.
    • try to execute it in python
    • try to execute if from R

    If both works you can try to do the same thing with the custom library.