3

I´m trying to run a python script from R using system() function:

system('D:/python script.py', wait=T)

The script runs well by itself (double click on it), but when I call it from R it doesn't run and I get the following message:

Warning message:
running command 'D:/python script.py' had status 127

When I try to run it using shell.exec('python script.py'), it runs perfectly. However, I need to wait the command to finish, to follow to the next line of the code. The argument 'wait' from system(), system2(), or shell() allows me to do that, but shell.exec() doesn't have a similar argument.

This is the code of my python script applying the Tabulate Area function from ArcGIS:

# Importing libraries
import arcpy
from arcpy import env
from arcpy.sa import *

# Check out the ArcGIS Spatial Analyst extension license
arcpy.CheckOutExtension("Spatial")

# Script arguments
Zone_Raster = "D:/Zone_Raster.tif"
Sp_Raster = "D:/Value_Raster.tif"
Tab_Area = "D:/Tab_Area.dbf"

# Tabulate area
TabulateArea(Zone_Raster,"VALUE",Sp_Raster,"VALUE",Tab_Area,"#")

I've also tried calling a .bat file where I pass the arguments to the python script as sys.argv. The .bat file runs perfectly by itself (double clic on it), but when I call it using system() or shell() it doesn't run and returns this message:

'python script.py' is not recognized as an internal or external command,
operable program or batch file

Does any body known how to run the python script or the .bat file using system(), system2(), or shell()? Or how I can make that shell.exec() waits the command to finish?

Thanks in advance,

Cheers,

Jaime

Squashman
  • 13,649
  • 5
  • 27
  • 36
Jaime
  • 111
  • 7
  • Why not try using the `reticulate` package to avoid a system call? – hrbrmstr Nov 02 '17 at 15:00
  • The script name has a space in it? `'D:/python script.py'` looks like you are trying to run a program named "python" with a parameter "script.py". I assume this is Windows? You'll need to escape the space. Try `'D:/python\\ script.py'` or `'"D:/python script.py"'` – MrFlick Nov 02 '17 at 15:16
  • Thanks MrFlick, I put that name on the example, but neither the orinal name of the script nor the directory have spaces. – Jaime Nov 02 '17 at 15:54
  • I´have tried with `py_run_file()` from `reticulate` but I get this error: `Error in initialize_python(required_module, use_environment) : Installation of Python not found, Python bindings not loaded` – Jaime Nov 02 '17 at 16:02
  • Thanks again MrFlick, you were right. I've made many attempts that I forgot that I changed the code to write the python scripts and they were creating with a space. Using `shell('"D:/python script.py"')` works perfect. – Jaime Nov 02 '17 at 17:10

0 Answers0