简体   繁体   English

使用 Reticulate 运行 Python 脚本和 Arguments

[英]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.我有一个 python 脚本,由我们的一位建筑师在这里开发。 I would like to execute it in R using the Reticulate package.我想使用Reticulate package 在R中执行它。 The script acts like an API to one of our systems.对于我们的一个系统,该脚本就像 API 一样。 I call it on my machine providing it with a number and it spits out a JSON file for me.我在我的机器上调用它并为其提供一个数字,它会为我吐出一个 JSON 文件。

I can see from looking at the documentation that it is possible to run the script using py_run_file(file) .从查看文档中可以看出,可以使用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.我可以使用它来运行脚本,但是当我添加 arguments 说它不知道文件在哪里时它失败了。 It all works perfectly via the command prompt and the file can be found when i don't include the arguments.这一切都通过命令提示符完美运行,当我不包含 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.我知道我可以将 cmd 提示符包装在 R 中,以通过 python 执行脚本,但想知道是否可以将命令提交给Reticulate并取回结果。 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?) py_run_file_impl(文件,本地,转换)中的错误:无法打开文件'C:/Users/foo/Documents/util/account_number.py -n 9998877'(它存在吗?)

Thank you very much for your help非常感谢您的帮助

I have found the answer here我在这里找到了答案

So i source the python script in R and have access to all its functions my environment.所以我在 R 中获取 python 脚本,并可以在我的环境中访问它的所有功能。 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谢谢

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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