John Smith
John Smith

Reputation: 2886

Using Reticulate to Run a Python Script with Arguments

I have a python script that has kindly been developed by one of our architects here. I would like to execute it in R using the Reticulate package. The script acts like an API to one of our systems. I call it on my machine providing it with a number and it spits out a JSON file for me.

I can see from looking at the documentation that it is possible to run the script using py_run_file(file). I am able to use this to run the script but it fails when I add arguments saying it does not know where the file is. It all works perfectly via the command prompt and the file can be found when i don't include the arguments.

I am aware I could just wrap the cmd prompt in R to execute the script via python but was wondering if its possible to just submit the command to Reticulate and get back the results. Below is a pseudo-code example

library(reticulate)
file = 'C:/Users/foo/Documents/util/account_number.py -n 9998877'
py_run_file(file)

Error in py_run_file_impl(file, local, convert) : Unable to open file 'C:/Users/foo/Documents/util/account_number.py -n 9998877' (does it exist?)

Thank you very much for your help

Upvotes: 4

Views: 2434

Answers (1)

John Smith
John Smith

Reputation: 2886

I have found the answer here

So i source the python script in R and have access to all its functions my environment. Applying that to my own problem above


library(reticulate)
file = 'C:/Users/foo/Documents/util/account_number.py'
source_python(file)
search_account('9998877')

I hope other people find this useful

Thanks

Upvotes: 3

Related Questions