You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

使用system2()运行Python脚本在R终端正常但RStudio异常的问题

问题:RStudio/Rgui中调用Python脚本失败,R终端正常

测试脚本与正常运行场景

测试用Python脚本test.py内容:

from sys import stdout

stdout.write("text out")

在R终端中使用system2()调用该脚本可正常输出结果:

python <- "C:/Users/username/Anaconda3/envs/general/python.exe"
script <- "C:/Users/username/Project Folder/test.py"
system2(command = python, args = shQuote(script), stdout = TRUE, stderr = FALSE)
# 输出:"text out"

异常场景

在RStudio中运行相同代码,返回空向量并伴随非零退出状态警告:

system2(command = python, args = shQuote(script), stdout = TRUE, stderr = FALSE)
# 输出:character(0)
# attr("status")
# [1] 1
# 警告信息:
# In system2(command = python, args = shQuote(script), stdout = TRUE,  :
#   running command '"C:/Users/username/Anaconda3/envs/general/python.exe" "C:/Users/username/Project Folder/test.py"' had status 1

已排查情况

  • 在Rgui中无警告,但同样返回空向量;
  • 即使修改Python脚本,改用print()输出或写入磁盘文件,结果仍一致;
  • 替换为system(paste(shQuote(python), shQuote(script)))调用,问题依旧;
  • RStudio中使用system2()执行系统命令或带参数的批处理文件正常,仅无法运行Python脚本;
  • 通过system2("printenv")对比发现,R终端会话中有部分环境变量未在RStudio中设置,但手动用Sys.setenv()补齐后问题仍未解决;sessionInfo()输出无明显差异;
  • 以管理员权限运行RStudio,问题仍存在。

背景关联

用户尝试过多个版本的R和RStudio,始终无法正常使用reticulate包,经排查问题根源在于reticulate:::python_config_impl()内部的system2()调用会触发上述相同警告,进而导致包初始化错误。

内容的提问来源于stack exchange,提问作者zephryl

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.16 16:04:52