[R] system() or shell() with python script

Duncan Murdoch murdoch at stats.uwo.ca
Tue Oct 20 12:55:33 CEST 2009


Paul Hiemstra wrote:
> Remko Duursma wrote:
>   
>> Hi all,
>>
>> I am having some problems calling a python script from R that resides
>> in a folder that is in the path (WindowsXP):
>>   
>>     
> Hi Remko,
>
> Some suggestions:
>
> 1. Try to see if the path that R has from a call to system is correct 
> (i.e. the same as from cmd):
>
> system("path")
>   

That won't work.  You would use Sys.getenv("PATH") for this.
> 2. Try calling it with python added in front:
>
> system("python script.py")
>
> 3. Add a shebang line to the top of your script like:
>
> #! c:/Program Files/Python/python.exe
>
> This tells the OS which program you want to use to run the script.
>   

That's unlikely to work on Windows.

If file associations are set to do it, then shell.exec("script.py") 
should work.  (That should do the same as double clicking the file in 
Explorer.)

Duncan
> cheers,
> Paul
>
> ps maybe superfluous, but try the python getopt package for reading 
> commandline arguments.
>   
>>   
>>     
>>> system("quickPadTool.py")
>>>     
>>>       
>> Warning message:
>> In system("quickPadTool.py") : quickPadTool.py not found
>>
>> # I also tried 'shell' (and shell.exec as well).
>>   
>>     
>>> shell("quickPadTool.py")
>>>     
>>>       
>> 'quickPadTool.py' is not recognized as an internal or external command,
>> operable program or batch file.
>> Warning message:
>> In shell("quickPadTool.py") :
>>   'quickPadTool.py' execution failed with error code 1
>>
>> I can run the script fine from a command window just fine, from the
>> same directory.
>>
>> Any pointers?
>>
>> thanks,
>> Remko
>>
>>
>>
>>
>>
>> -------------------------------------------------
>> Remko Duursma
>> Post-Doctoral Fellow
>>
>> Centre for Plants and the Environment
>> University of Western Sydney
>> Hawkesbury Campus
>> Richmond NSW 2753
>>
>> Dept of Biological Science
>> Macquarie University
>> North Ryde NSW 2109
>> Australia
>>
>> Mobile: +61 (0)422 096908
>> www.remkoduursma.com
>>
>> ______________________________________________
>> R-help at r-project.org mailing list
>> https://stat.ethz.ch/mailman/listinfo/r-help
>> PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
>> and provide commented, minimal, self-contained, reproducible code.
>>   
>>     
>
>
>




More information about the R-help mailing list