因为做实验需要用到R语言,所以安装rpy2在python中使用。
相关环境:
python 2.7.14
R 3.4.3
rpy2 2.8.6
rpy2安装遇到的问题:
起初是打算virtualenv中已有的一个虚拟环境中安装rpy2,使用pip安装rpy2,检测到你用的是python2.x之后,就会安装失败跳出:
rpy2 is no longer supporting Python < 3. Consider using an older rpy2 release when using an older Python release.
然后,尝试手动安装低版本兼容python2.x的rpy2,也总是失败。
经人指点,开始尝试用conda来搭环境,一次成功(不得不说conda确实比pip更亲民!)。
install packages遇到的问题:
当我们刚刚安装完rpy2的时候,它本身只包含最基本的packages,如果想要实现更多功能,就需要我们自己install了。
比如我,需要genlasso来搞事情。正常情况下,我们在python中嵌入
r_script = '''
install.packages("genlasso",dependencies=TRUE)
'''
r(r_script)
后续就可以使用genlasso了。但是,这个过程中出现了genlasso依赖包igraph无法成功编译的问题:
foreign-graphml.c: In function ‘igraph_write_graph_graphml’:
foreign-graphml.c:1408:46: error: expected ‘)’ before ‘GRAPHML_NAMESPACE_URI’
ret=fprintf(outstream, "<graphml xmlns="" GRAPHML_NAMESPACE_URI ""\n");
^~~~~~~~~~~~~~~~~~~~~
foreign-graphml.c:1412:59: error: expected ‘)’ before ‘GRAPHML_NAMESPACE_URI’
ret=fprintf(outstream, " xsi:schemaLocation="" GRAPHML_NAMESPACE_URI "\n");
^~~~~~~~~~~~~~~~~~~~~
foreign-graphml.c:1414:38: error: expected ‘)’ before ‘GRAPHML_NAMESPACE_URI’
ret=fprintf(outstream, " " GRAPHML_NAMESPACE_URI "/1.0/graphml.xsd">\n");
^~~~~~~~~~~~~~~~~~~~~
/usr/lib/R/etc/Makeconf:132: recipe for target 'foreign-graphml.o' failed
make: *** [foreign-graphml.o] Error 1
ERROR: compilation failed for package ‘igraph’
此时,我并不知道怎么在当前环境下继续安装igraph。因为我菜,所以我用了最笨的方法,又安装了R语言。虽然在R语言的环境下安装,依然会出现这样的问题。但是,我可以在/tmp目录下找到 igraph_1.2.1.tar.gz的安装包呀。又在网上找到了
点击打开链接(https://github.com/igraph/rigraph/issues/213)里@liuyifang的回复(假装能@到)
1.按照点击打开链接(https://github.com/igraph/igraph/commit/9acfa54fa6b3d182fe458434a497f4e9b5c39955)中的步骤更改安装包中的src/foreign-graphml.c文件,一般情况下,这里是没问题的。
#include "igraph_memory.h"
#include <stdarg.h> /* va_start & co */
#define GRAPHML_NAMESPACE_URI "http://graphml.graphdrawing.org/xmlns"
#if HAVE_LIBXML == 1
#include <libxml/encoding.h>
#include <libxml/parser.h>
//#define GRAPHML_NAMESPACE_URI "http://graphml.graphdrawing.org/xmlns"
xmlEntity blankEntityStruct = {
#ifndef XML_WITHOUT_CORBA
0,
2.然后按照点击打开链接(https://github.com/igraph/igraph/pull/1020/files)中的方法去改写src/foreign-graphml.c文件。
ret=fprintf(outstream, "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n");
if (ret<0) IGRAPH_ERROR("Write failed", IGRAPH_EFILE);
// ret=fprintf(outstream, "<graphml xmlns=\"" GRAPHML_NAMESPACE_URI "\"\n");
ret=fprintf(outstream, "<graphml xmlns=\"%s\"\n", GRAPHML_NAMESPACE_URI);
if (ret<0) IGRAPH_ERROR("Write failed", IGRAPH_EFILE);
ret=fprintf(outstream, " xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\"\n");
if (ret<0) IGRAPH_ERROR("Write failed", IGRAPH_EFILE);
// ret=fprintf(outstream, " xsi:schemaLocation=\"" GRAPHML_NAMESPACE_URI "\n");
ret=fprintf(outstream, " xsi:schemaLocation=\"%s\n", GRAPHML_NAMESPACE_URI);
if (ret<0) IGRAPH_ERROR("Write failed", IGRAPH_EFILE);
// ret=fprintf(outstream, " " GRAPHML_NAMESPACE_URI "/1.0/graphml.xsd\">\n");
ret=fprintf(outstream, " %s/1.0/graphml.xsd\">\n", GRAPHML_NAMESPACE_URI);
if (ret<0) IGRAPH_ERROR("Write failed", IGRAPH_EFILE);
ret=fprintf(outstream, "<!-- Created by igraph -->\n");
if (ret<0) IGRAPH_ERROR("Write failed", IGRAPH_EFILE);
3.通过点击打开链接(http://kbroman.org/pkg_primer/pages/build.html)的指示安装改写后的igraph_1.2.1.tar.gz的安装包。
R CMD INSTALL igraph_1.2.1.tar.gz
接下来,安装genlasso就不再是问题。
第一次写博客,希望给大家带来帮助。虽然,2020年1月1日就将终止python2.7的支持,万一这期间还有人遇到我这种问题呢,是吧!!!选择文章类型?原创、转载、翻译。我这算“半个转载”+“半个翻译”?那写原创好了,膨胀一下