Skip to main content
 首页 » 编程设计

python之RKeras "unknown url type: https"错误 six.urlretrieve(来自 R 的 Python 代码)

2025年01月19日9pengyingh

TL;DR :-) 获取 https url 在 python 和 R 中都有效,但当 python 代码从 R 运行时则无效。


在运行 RKeras 包文档中的一些代码时,我多次遇到 fatal error :"unknown url type: https" 问题起源于用 Python 实现的 Keras。

我深入研究了这个问题,发现这是在 Keras 调用模块 six 中的函数 urlretrieve 以从 https url 检索数据时发生的。

然后我在 iPython 中测试了代码,发现它运行良好。

<!-- language: lang-python --> 
from six.moves.urllib.request import urlretrieve 
urlretrieve(url="https://www.google.com") 

然后我尝试在 R 中做同样的事情但失败了

<!-- language: lang-r --> 
library(reticulate) 
py_run_string('from six.moves.urllib.request import urlretrieve') 
py_run_string("urlretrieve(url='https://www.google.com')") 

但是,同样的事情在 R 中使用纯 http

<!-- language:lang-r --> 
py_run_string("urlretrieve(url='http://www.google.com')") 

郑重声明,https 在我的 R 中使用 httr 等包工作正常。

我在这里完全超出了我的深度。会发生什么?

这是关于我的环境的一些输出

回复:

sessionInfo() 
# R version 3.4.4 (2018-03-15) 
# Platform: x86_64-pc-linux-gnu (64-bit) 
# Running under: Ubuntu 16.04.4 LTS 
# other attached packages: 
# [1] reticulate_1.6 kerasR_0.8.0   

和 Python(从 R 看)

py_config() 
 
version:        3.5.2 |Anaconda custom (64-bit)|  
(default, Jul  2 2016, 17:53:06)  [GCC 4.4.7 20120313 (Red Hat 4.4.7-1)] 

非常感谢您的时间和精力。


编辑:R 中的更多调试信息:

py_run_string("from OpenSSL import SSL") 
# ImportError: /xxx/python3.5/lib-dynload/_ssl.so: undefined symbol: SSLv2_method 

请您参考如下方法:

目前,我发布了针对我的特定问题的部分解决方案。这不会解决 Reticulate 和 SSL 的一般问题,但它是一个不错的临时解决方法,可以帮助任何使用KerasR 或 Keras 并发现它无法下载模型和数据库的人强>

Keras/KerasR 使用缓存来避免将来下载同一对象。在我的例子中,它是 ~/.keras/

于是从控制台复制下载失败的URL,用浏览器下载对象并保存到keras的缓存目录。

cache for models : ~/.keras/models/ 
cache for datasets : ~/.keras/datasets/ 
(where ~ is my home directory) 

当然,这是一种解决方法,我仍然期待有人发布适用于系统范围的正确解决方案。