如何在R中指定Python安装路径?解决birdnetR调用报错问题
问题描述
Windows系统安装Python 3.14后,R包birdnetR安装、加载均正常,但执行model <- birdnet_model_tflite()时出现报错:
Error : could not find a Python environment for C:/ In addition:
Warning message: In normalizePath(path.expand(path), winslash,
mustWork) :
path[1]="C:/Users/adamd/AppData/Local/R/cache/R/reticulate/uv/cache/builds-v0/.tmplxgNVv/Scripts":
The system cannot find the path specified Error in value[3L] :
Failed to evaluate Python path: Installation of Python not found,
Python bindings not loaded.
通过命令提示符获取Python安装路径:
python -c "import os, sys; print(os.path.dirname(sys.executable))"
得到路径:C:\Users\adamd\AppData\Local\Programs\Python\Python314
使用reticulate的use_python指定路径时收到警告:
Warning message: The request to
use_python("C:/Users/adamd/AppData/Local/Programs/Python/Python314/python.exe")
will be ignored because the environment variable RETICULATE_PYTHON is
set to "managed"
取消环境变量后强制设置,执行py_config()又报错:
Error in value[3L] :
Failed to evaluate Python path: Python module birdnet.models was not found. Detected Python configuration:
解决方案
1. 清除reticulate的"managed"环境变量影响
重启R/RStudio后,在会话开头执行:
Sys.setenv(RETICULATE_PYTHON = "") Sys.unsetenv("RETICULATE_PYTHON")
2. 安装Python依赖包birdnetlib
打开命令提示符,执行以下命令为指定Python环境安装所需依赖:
cd "C:\Users\adamd\AppData\Local\Programs\Python\Python314" .\python.exe -m pip install birdnetlib
3. 绑定并验证Python环境
在R中执行:
library(reticulate) # 指定Python路径 use_python("C:/Users/adamd/AppData/Local/Programs/Python/Python314/python.exe", required = TRUE) # 验证环境配置 py_config() # 检查birdnet模块是否可用 py_module_available("birdnet.models")
4. 重新运行birdnetR函数
library(birdnetR) model <- birdnet_model_tflite()
内容的提问来源于stack exchange,提问作者adkane

