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

在RStudio中导入.py文件时遭遇dir.exists文件名转换过长错误

解决reticulate导入Python文件时的dir.exists(x)编码错误

问题描述

在RStudio中使用reticulate包的source_python()导入.py文件时,出现以下错误:

Error in dir.exists(x) : file name conversion problem -- name too long?

已尝试使用纯英文路径,且list.files()能正常显示目标文件,但问题仍存在。相关代码、回溯信息及会话信息如下:

代码片段

library(reticulate)
library(tidyverse)
library(forcats)

setwd("C:/Users/Κατερίνα/Nextcloud/Multipic_Exp.1")

source_python("euler.py") # 此处触发错误

回溯信息(traceback())

12: dir.exists(x)
11: utils::file_test("-d", version_dir)
10: read_python_versions_from_registry("HCU", key = "PythonCore")
9: rbind(read_python_versions_from_registry("HCU", key = "PythonCore"), 
       read_python_versions_from_registry("HLM", key = "PythonCore"), 
       windows_registry_anaconda_versions())
8: py_versions_windows()
7: python_conda_versions()
6: py_discover_config(required_module, use_environment)
5: (function() {
       op <- options(reticulate.python.initializing = TRUE)
       on.exit(options(op), add = TRUE)
       py_discover_config(required_module, use_environment)
   })()
4: initialize_python(required_module, use_environment)
3: ensure_python_initialized()
2: py_run_file(file, local = FALSE, convert = convert)
1: source_python("euler.py")

会话信息(sessionInfo())

R version 4.1.0 (2021-05-18)
Platform: x86_64-w64-mingw32/x64 (64-bit)
Running under: Windows 10 x64 (build 19044)

Matrix products: default

locale:
[1] LC_COLLATE=Greek_Greece.1253  LC_CTYPE=Greek_Greece.1253    LC_MONETARY=Greek_Greece.1253
[4] LC_NUMERIC=C                  LC_TIME=Greek_Greece.1253    

attached base packages:
[1] stats     graphics  grDevices utils     datasets  methods   base     

other attached packages:
 [1] forcats_0.5.1   stringr_1.4.0   dplyr_1.0.7     purrr_0.3.4     readr_1.4.0     tidyr_1.1.3     tibble_3.1.2   
 [8] ggplot2_3.3.5   tidyverse_1.3.1 reticulate_1.26

loaded via a namespace (and not attached):
 [1] Rcpp_1.0.9       cellranger_1.1.0 pillar_1.6.1     compiler_4.1.0   dbplyr_2.1.1     tools_4.1.0     
 [7] lubridate_1.8.0  jsonlite_1.8.4   lifecycle_1.0.3  gtable_0.3.0     lattice_0.20-44  pkgconfig_2.0.3 
[13] png_0.1-7        rlang_1.0.6      reprex_2.0.1     Matrix_1.3-3     cli_3.4.1        DBI_1.1.1       
[19] rstudioapi_0.13  haven_2.4.1      xml2_1.3.2       withr_2.4.2      httr_1.4.2       fs_1.5.2        
[25] hms_1.1.0        generics_0.1.0   vctrs_0.5.1      rprojroot_2.0.2  grid_4.1.0       tidyselect_1.1.1
[31] here_1.0.1       glue_1.6.2       R6_2.5.1         fansi_0.5.0      readxl_1.3.1     modelr_0.1.8    
[37] magrittr_2.0.3   scales_1.1.1     backports_1.2.1  ellipsis_0.3.2   rvest_1.0.2      assertthat_0.2.1
[43] colorspace_2.0-2 utf8_1.2.1       stringi_1.7.6    munsell_0.5.0    broom_0.7.8      crayon_1.4.1    

问题根源

从回溯信息可以看出,错误并非来自目标Python文件的路径,而是reticulate在读取Windows注册表中的Python版本信息时,因系统区域设置(希腊语Greek_Greece.1253)与注册表路径的编码不兼容,导致文件名转换失败。

解决方案

1. 手动指定Python解释器路径

绕过reticulate的自动检测机制,直接指定Python可执行文件路径,避免读取注册表:

# 替换为你的Python实际路径,例如:
use_python("C:/Python39/python.exe", required = TRUE)
# 然后再导入文件
source_python("euler.py")

2. 临时修改R的区域设置

在R会话中临时切换到英文区域设置,解决编码冲突:

# 临时设置区域为英文
Sys.setlocale(category = "LC_ALL", locale = "English_United States.1252")
# 重新运行导入代码
source_python("euler.py")

3. 更新reticulate包到最新版本

旧版本的reticulate可能存在Windows注册表编码处理的bug,更新包可能修复问题:

install.packages("reticulate")
library(reticulate)

4. 检查Python安装路径

确保Python的安装路径及环境变量中没有非ASCII字符(如希腊语、中文等),如果有,建议重新安装Python到纯英文路径下。

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.08.09 12:05:16