python2.x环境下对rpy2包使用的前期配置、install packages (compilation failed for package ‘igraph’)小贴士

因为做实验需要用到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的支持,万一这期间还有人遇到我这种问题呢,是吧!!!大笑选择文章类型?原创、转载、翻译。我这算“半个转载”+“半个翻译”?那写原创好了,膨胀一下微笑

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值