简体   繁体   中英

Problem running successive Python scripts using 'Reticulate' function in R

I'm having a problem running two successive python scripts in a single R script that calls the reticulate function. When I run them separately, it works perfectly. Am I missing something? Do I need some sort of function that closes the previous py_run before executing the second one? Note that I am working with the ArcGIS 'arcpy' tools.

Here is an example. If I run this in an Rstudio session:

        use_python("C:/Python27/ArcGISx6410.7/python.exe", required = T)
        import("arcpy")
        import("os")
        import("glob")
        import("re")
        import("math")

        pythonRun1 = paste0("modelName = ", "'", modelName, "'")

        py_run_string(pythonRun1)
        py_run_file("C:/GIS/Bathymetry_UdeM/Radial_Linear_Mean_R/Output/Workflow/InterpolateResults.py")

and then this in a second Rstudio session:

        use_python("C:/Python27/ArcGISx6410.7/python.exe", required = T)
        import("arcpy")
        import("arcpy.sa")
        import("os")
        import("glob")
        import("re")
        import("math")

        pythonRun2 = paste0("modelName = ", "'", modelName, "'")

        py_run_string(pythonRun2)
        py_run_file("C:/GIS/Bathymetry_UdeM/Radial_Linear_Mean_R/Output/Workflow/FocalMean.py")

it works perfectly. But when I'm trying to successively execute these two code blocks:

        use_python("C:/Python27/ArcGISx6410.7/python.exe", required = T)
        import("arcpy")
        import("os")
        import("glob")
        import("re")
        import("math")

        pythonRun1 = paste0("modelName = ", "'", modelName, "'")

        py_run_string(pythonRun1)
        py_run_file("C:/GIS/Bathymetry_UdeM/Radial_Linear_Mean_R/Output/Workflow/InterpolateResults.py")

        #########################################################################################################

        use_python("C:/Python27/ArcGISx6410.7/python.exe", required = T)
        import("arcpy")
        import("arcpy.sa")
        import("os")
        import("glob")
        import("re")
        import("math")

        pythonRun2 = paste0("modelName = ", "'", modelName, "'")

        py_run_string(pythonRun2)
        py_run_file("C:/GIS/Bathymetry_UdeM/Radial_Linear_Mean_R/Output/Workflow/FocalMean.py")

I get noData as my final output. The second portion of the script does not callback any error technically speaking, but the code does not execute what it is supposed to.

Does anyone have an idea as to what may cause the difference between these two methods (ie, separate and successive py_run execution)?

Thanks everyone!

Try constructing a MRWE . It's hard to answer a question like this without being able to run the code. Here's a couple of starting points for troubleshooting, though.

  1. I would not advise running reticulate::use_python twice. It is my understanding that once the Python interpreter is embedded into the R session it cannot be changed, so when this is called a second time at best it can have no effect, and at worst it could be causing issues.

  2. You don't show any code where a value is bound to model_name , is it supposed to be changed in-between the two blocks of code?

  3. Is there any state that is being modified in the first code block that is affecting the evaluation of the second code block? For example, if a variable is created in the first code block that is shadowing a variable being used in the second code block.

  4. Try running the analogous code directly in Python and ensure that that works correctly. If not then it will be easier to debug in that environment.

Good luck!

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM