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

Windows下使用precommit包报错找不到Rscript可执行文件

问题描述

我已按照官方指引安装precommit包及miniconda环境,但运行过程中持续触发错误:Executable C:/PROGRA~1/R/R-42~1.0\bin\Rscript not found,在系统终端中可正常调用Rscript命令。

版本信息

pre-commit version: 2.19.0
git --version: git version 2.36.1.windows.1
sys.version:
    3.8.13 | packaged by conda-forge | (default, Mar 25 2022, 05:59:00) [MSC v.1929 64 bit (AMD64)]
sys.executable: C:\Users\pinto\AppData\Local\r-miniconda\envs\r-precommit\python.exe
os.name: nt
sys.platform: win32

错误信息

An unexpected error has occurred: CalledProcessError: command: ('C:/PROGRA~1/R/R-42~1.0\\bin\\Rscript', '--vanilla', '-e', '    options(install.packages.compile.from.source = "never")\n                prefix_dir <- \'C:\\\\Users\\\\pinto\\\\.cache\\\\pre-commit\\\\repom0jg06ie\'\n            options(\n                repos = c(CRAN = "https://cran.rstudio.com"),\n                renv.consent = TRUE\n            )\n            source("renv/activate.R")\n            renv::restore()\n            activate_statement <- paste0(\n              \'suppressWarnings({\',\n              \'old <- setwd("\', getwd(), \'"); \',\n              \'source("renv/activate.R"); \',\n              \'setwd(old); \',\n              \'renv::load("\', getwd(), \'");})\'\n            )\n            writeLines(activate_statement, \'activate.R\')\n            is_package <- tryCatch(\n              {\n                  path_desc <- file.path(prefix_dir, \'DESCRIPTION\')\n                  suppressWarnings(desc <- read.dcf(path_desc))\n                  "Package" %in% colnames(desc)\n              },\n              error = function(...) FALSE\n            )\n            if (is_package) {\n                renv::install(prefix_dir)\n            }\n            \n    ')
return code: 1
expected return code: 0
stdout:
    Executable `C:/PROGRA~1/R/R-42~1.0\bin\Rscript` not found
stderr: (none)
Traceback (most recent call last):
  File "C:\Users\pinto\AppData\Local\r-miniconda\envs\r-precommit\lib\site-packages\pre_commit\error_handler.py", line 73, in error_handler
    yield
  File "C:\Users\pinto\AppData\Local\r-miniconda\envs\r-precommit\lib\site-packages\pre_commit\main.py", line 361, in main
    return hook_impl(
  File "C:\Users\pinto\AppData\Local\r-miniconda\envs\r-precommit\lib\site-packages\pre_commit\commands\hook_impl.py", line 238, in hook_impl
    return retv | run(config, store, ns)
  File "C:\Users\pinto\AppData\Local\r-miniconda\envs\r-precommit\lib\site-packages\pre_commit\commands\run.py", line 414, in run
    install_hook_envs(to_install, store)
  File "C:\Users\pinto\AppData\Local\r-miniconda\envs\r-precommit\lib\site-packages\pre_commit\repository.py", line 223, in install_hook_envs
    _hook_install(hook)
  File "C:\Users\pinto\AppData\Local\r-miniconda\envs\r-precommit\lib\site-packages\pre_commit\repository.py", line 79, in _hook_install
    lang.install_environment(
  File "C:\Users\pinto\AppData\Local\r-miniconda\envs\r-precommit\lib\site-packages\pre_commit\languages\r.py", line 139, in install_environment
    cmd_output_b(
  File "C:\Users\pinto\AppData\Local\r-miniconda\envs\r-precommit\lib\site-packages\pre_commit\util.py", line 146, in cmd_output_b
    raise CalledProcessError(returncode, cmd, retcode, stdout_b, stderr_b)
pre_commit.util.CalledProcessError: command: ('C:/PROGRA~1/R/R-42~1.0\\bin\\Rscript', '--vanilla', '-e', '    options(install.packages.compile.from.source = "never")\n                prefix_dir <- \'C:\\\\Users\\\\pinto\\\\.cache\\\\pre-commit\\\\repom0jg06ie\'\n            options(\n                repos = c(CRAN = "https://cran.rstudio.com"),\n                renv.consent = TRUE\n            )\n            source("renv/activate.R")\n            renv::restore()\n            activate_statement <- paste0(\n              \'suppressWarnings({\',\n              \'old <- setwd("\', getwd(), \'"); \',\n              \'source("renv/activate.R"); \',\n              \'setwd(old); \',\n              \'renv::load("\', getwd(), \'");})\'\n            )\n            writeLines(activate_statement, \'activate.R\')\n            is_package <- tryCatch(\n              {\n                  path_desc <- file.path(prefix_dir, \'DESCRIPTION\')\n                  suppressWarnings(desc <- read.dcf(path_desc))\n                  "Package" %in% colnames(desc)\n              },\n              error = function(...) FALSE\n            )\n            if (is_package) {\n                renv::install(prefix_dir)\n            }\n            \n    ')
return code: 1
expected return code: 0
stdout:
    Executable `C:/PROGRA~1/R/R-42~1.0\bin\Rscript` not found
stderr: (none)
sessionInfo()
#> R version 4.2.0 (2022-04-22 ucrt)
#> Platform: x86_64-w64-mingw32/x64 (64-bit)
#> Running under: Windows 10 x64 (build 19042)
#> 
#> Matrix products: default
#> 
#> locale:
#> [1] LC_COLLATE=en_GB.utf8  LC_CTYPE=en_GB.utf8    LC_MONETARY=en_GB.utf8
#> [4] LC_NUMERIC=C           LC_TIME=en_GB.utf8    
#> 
#> attached base packages:
#> [1] stats     graphics  grDevices utils     datasets  methods   base     
#> 
#> loaded via a namespace (and not attached):
#>  [1] rstudioapi_0.13   knitr_1.39        magrittr_2.0.3    R.cache_0.15.0   
#>  [5] rlang_1.0.2       fastmap_1.1.0     fansi_1.0.3       stringr_1.4.0    
#>  [9] styler_1.7.0      highr_0.9         tools_4.2.0       xfun_0.31        
#> [13] R.oo_1.24.0       utf8_1.2.2        cli_3.3.0         withr_2.5.0      
#> [17] htmltools_0.5.2   ellipsis_0.3.2    yaml_2.3.5        digest_0.6.29    
#> [21] tibble_3.1.7      lifecycle_1.0.1   crayon_1.5.1      purrr_0.3.4      
#> [25] R.utils_2.11.0    vctrs_0.4.1       fs_1.5.2          glue_1.6.2       
#> [29] evaluate_0.15     rmarkdown_2.14    reprex_2.0.1      stringi_1.7.6    
#> [33] compiler_4.2.0    pillar_1.7.0      R.methodsS3_1.8.1 pkgconfig_2.0.3
问题原因

报错核心是pre-commit读取到的Rscript 8.3格式短路径C:/PROGRA~1/R/R-42~1.0\bin\Rscript和实际R安装路径不匹配:

  • 安装的UCRT版本R 4.2.0默认路径为C:\Program Files\R\R-4.2.0,但pre-commit缓存了旧的无效短路径,通常是R升级/重装后,注册表或环境变量残留旧记录,pre-commit未自动更新导致。
  • 系统终端可正常调用Rscript,是因为终端读取的是系统PATH中的最新有效路径,和pre-commit内部获取R路径的来源不一致。
解决方案

按以下顺序操作即可修复:

  • 清除pre-commit旧缓存,执行命令删除缓存目录:
    rmdir /s /q %USERPROFILE%\.cache\pre-commit
    
  • 显式指定Rscript路径给pre-commit,打开系统环境变量设置,新增系统级环境变量:
    • 变量名:R_SCRIPT_PATH
    • 变量值:Rscript实际完整路径,例如C:\Program Files\R\R-4.2.0\bin\Rscript.exe
  • 重启所有终端、IDE(包括RStudio、Git Bash等),确保新环境变量生效
  • 重新执行pre-commit安装命令,重新初始化钩子环境即可。

如果上述操作后仍报错,可在项目根目录的.pre-commit-config.yaml中,给R相关钩子额外增加Rscript路径配置,强制指定执行路径。


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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.09.02 21:45:44