【问题标题】:Calling an Rscript using powershell and python使用 powershell 和 python 调用 Rscript
【发布时间】:2021-10-27 19:00:29
【问题描述】:

我用 python 编写代码,我的同事用 R 编写代码。我正在尝试调用他编写的 Rscript 文件以将其合并到 python 应用程序中。但是,当使用与 python 子进程库 [subprocess.run() /subprocess.call()/ subprocess.Popen()/ subprocess.check_output()] 完全相同的命令时,我可以使用窗口的 powershell 成功调用 Rscript . 在 power shell 中工作的命令是:

Rscript C:/Users/file/path/script_name.R

Rscript 是我的 PATH 环境中的一个变量,它引用了我的 Rscript.exe 文件路径。我试过用它代替完整的文件路径,但这不会改变结果。 在我的 python 代码中,子进程调用如下所示:

subprocess.check_output('Rscript C:/Users/file/path/script_name.R, shell=True)

运行此代码会在我的 python IDE 中显示以下错误消息。我发现这很不寻常,原因有三个。第一个是程序声称多次停止执行但继续执行并多次遇到相同的错误。其次是包'ggplot2'和'scales'肯定是导入的,因为代码在Rstudio和powershell中都可以工作而不会触发这个错误。还有一行提到“rstudioapi”有问题,但是这个包没有出现在文件中的任何地方,而且我没有使用 Rstudio。我正在寻找一种修复子进程调用的方法,或者一种从python打开powershell窗口并将工作命令输入终端窗口的方法。我对此感到困惑,我认为我不了解子流程功能。

Error in loadNamespace(name) : there is no package called 'rstudioapi'
Calls: ReqFunc ... loadNamespace -> withRestarts -> withOneRestart -> doWithOneRestart
Execution halted
Error in library(ggplot2) : there is no package called 'ggplot2'
Calls: ReqFunc -> source -> withVisible -> eval -> eval -> library
Execution halted
Error in library(ggplot2) : there is no package called 'ggplot2'
Calls: ReqFunc -> source -> withVisible -> eval -> eval -> library
Execution halted
Error in library(ggplot2) : there is no package called 'ggplot2'
Calls: ReqFunc -> source -> withVisible -> eval -> eval -> library
Execution halted
Fatal error: cannot open file 'script_name.R': No such file or directory

Error in library(ggplot2) : there is no package called 'ggplot2'
Calls: ReqFunc -> source -> withVisible -> eval -> eval -> library
Execution halted
Error in library(ggplot2) : there is no package called 'ggplot2'
Calls: ReqFunc -> source -> withVisible -> eval -> eval -> library
Execution halted
Error in library(ggplot2) : there is no package called 'ggplot2'
Calls: ReqFunc -> source -> withVisible -> eval -> eval -> library
Execution halted
Error in library(ggplot2) : there is no package called 'ggplot2'
Calls: ReqFunc -> source -> withVisible -> eval -> eval -> library
Execution halted
Error in library(ggplot2) : there is no package called 'ggplot2'
Calls: ReqFunc -> source -> withVisible -> eval -> eval -> library
Execution halted
Error in contrib.url(repos, "source") : 
  trying to use CRAN without setting a mirror
Calls: install.packages -> contrib.url
Execution halted
Error in contrib.url(repos, "source") : 
  trying to use CRAN without setting a mirror
Calls: install.packages -> contrib.url
Execution halted
Error in library(ggplot2) : there is no package called 'ggplot2'
Calls: ReqFunc -> source -> withVisible -> eval -> eval -> library
Execution halted
Error in library(ggplot2) : there is no package called 'ggplot2'
Calls: ReqFunc -> source -> withVisible -> eval -> eval -> library
Execution halted
Error in library(ggplot2) : there is no package called 'ggplot2'
Calls: ReqFunc -> source -> withVisible -> eval -> eval -> library
Execution halted
Error in library(scales) : there is no package called 'scales'
Calls: ReqFunc -> source -> withVisible -> eval -> eval -> library
Execution halted
Error in library(scales) : there is no package called 'scales'
Calls: ReqFunc -> source -> withVisible -> eval -> eval -> library
Execution halted
Error in library(scales) : there is no package called 'scales'
Calls: ReqFunc -> source -> withVisible -> eval -> eval -> library
Execution halted
Error in library(ggplot2) : there is no package called 'ggplot2'
Calls: ReqFunc -> source -> withVisible -> eval -> eval -> library
Execution halted

ReqFunc 是脚本中函数的名称

【问题讨论】:

  • 您可能在不同的环境或版本中运行 Python 或 R。在可以运行 Rscript 的 PowerShell 中,运行 python C:\path\to\script_name.py,假设 python` 在你的 PATH 变量中。
  • 该函数提到一些错误来自source(),因此脚本文件可能正在运行来自其他脚本文件的代码。这些文件可能引用“rstudioapi”。它们需要从命令行重新编写。看起来代码有install.packages(),这在你想从命令行重复运行的脚本中是不好的。没有任何形式的reproducible code,很难真正提供帮助。

标签: python r subprocess


【解决方案1】:

我找到了问题的答案。我发现的第一件事是 subprocess.check_output 记录了我的 python 控制台的整个日志,因此在尝试调试我的代码时,所有重复的“执行停止”消息都是我的活动的不同实例。 2,将 Rscript 作为子进程运行的调用失败,因为默认情况下,它调用的是命令提示符而不是 Powershell。这已使用以下代码行修复。

subprocess.check_output(["powershell.exe",command])

【讨论】:

  • 非常奇怪,Rscript 在 CMD.exe 或 PowerShell.exe 中的工作方式应该相同。很高兴能得到上述问题的答案。 PowerShell 可能在不同的环境中运行。
  • 在我解决问题之前,在同一环境中从 powershell 运行 python 脚本会产生类似的错误。
猜你喜欢
  • 2017-06-08
  • 1970-01-01
  • 2013-04-25
  • 2015-12-28
  • 2015-07-03
  • 2017-12-19
  • 2016-06-12
  • 1970-01-01
  • 2015-04-15
相关资源
最近更新 更多