简体   繁体   English

Python 中 subprocess.call() 的 FileNotFoundError

[英]FileNotFoundError on subprocess.call() in Python

I am trying to run R script using Python. I using subprocess.call function to achieve this.我正在尝试使用 Python 运行 R 脚本。我使用 subprocess.call function 来实现此目的。 As suggested in other posts I have tried these different codes:正如其他帖子中所建议的,我尝试了这些不同的代码:

Code1代码1

subprocess.call(['Rscript', '--vanilla', 'C:/Users/siddh/Downloads/R_script_BCA.R'])

Code 2代码 2

subprocess.Popen(['Rscript', '--vanilla', 'C:/Users/siddh/Downloads/R_script_BCA.R'])

Error for both两者都出错

FileNotFoundError: [WinError 2] The system cannot find the file specified

Code 3代码 3

subprocess.Popen('Rscript --vanilla C:/Users/siddh/Downloads/R_script_BCA.R', shell=True)

Running code 3 just shows the following and nothing happens运行代码 3 只显示以下内容,没有任何反应

<Popen: returncode: None args: 'Rscript --vanilla C:/Users/siddh/Downloads/R...>

The following code worked fine when used in command prompt/PowerShell以下代码在命令提示符/PowerShell 中使用时运行良好

Rscript --vanilla "C:/Users/siddh/Downloads/R_script_BCA.R"

This can happen if Rscript wasn't found in the PATH environ variable.如果在PATH环境变量中找不到 Rscript,就会发生这种情况。 Put the full path to Rscript, sort of:将完整路径放入 Rscript,类似于:

subprocess.call([
    r'C:\Program Files\R\R-4.2.1\bin\Rscript',    # put here the path to your Rscript
    '--vanilla', 
    'C:/Users/siddh/Downloads/R_script_BCA.R'
])

Or add the path before running subprocess.call :或者在运行subprocess.call之前添加路径:

import os
os.environ['PATH'] += ';' + r'C:\Program Files\R\R-4.2.1\bin'   # replace with your real path to Rscript

To see if you have or not the path to Rscript in the PATH inside of the running python:查看运行 python 的PATH中是否有 Rscript 的路径:

import os
for p in os.environ['PATH'].split(';'):
    print(p)

To find the path to your Rscript in PowerShell:要在 PowerShell 中找到您的 Rscript 的路径:

Get-Command Rscript | Select-Object Source

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

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