LookupError:embeded python下的未知编码'big5'

时间:2013-03-01 16:47:55

标签: python c r

我正在构建一个R扩展,其中包含一个嵌入式python。

现在一切顺利,除了python找不到我需要的编码。当我做涉及' big5'的事情时,它会不断抛出LookupError。但是,如果我构建一个独立的c ++应用程序,python解释器会找到编码并停止抛出错误。

test.cpp表示c:

中的正常独立示例
#include <Python.h>

int main(int argc, char* argv[]) {
  Py_SetProgramName("test");  /* optional but recommended */
    Py_Initialize();
    PyRun_SimpleString(
            "import codecs\n"
            "f = codecs.open('big5_encoded_file', encoding='big5', mode='r')"
            );
    Py_Finalize();
    return 0;
}
R扩展名

testr.cpp

#include <R.h>
#include <Rdefines.h>
#include <Python.h>

extern "C" SEXP testpy();

SEXP testpy() {
  Py_SetProgramName("test");  /* optional but recommended */
    Py_Initialize();
    PyRun_SimpleString(
            "import codecs\n"
            "f = codecs.open('big5_encoded_file', encoding='big5', mode='r')"
            );
    Py_Finalize();
    return R_NilValue;
}

ubuntu 12.10上的Makefile

all: test testr.so

test: test.cpp
  g++ test.cpp -o test -I/usr/include/python2.7 -lpython2.7


testr.so: testr.cpp
    R CMD SHLIB testr.cpp

./test正常运行,但Rscript -e "dyn.load('testr.so');.Call('testpy')"会产生&#34; LookupError:未知编码:big5&#34;

由于

- 编辑 -

要构建testr.so,请设置:

export PKG_CXXFLAGS=-I/usr/include/python2.7
export PKG_LIBS=-lpython2.7

1 个答案:

答案 0 :(得分:0)

我注意到这是一个链接问题。

我在嵌入式python中尝试了import encodings.big5,但发生了undefined reference的错误。 http://bugs.python.org/issue4434中的解决方案对我有用:

  在PyInitialize()之前

我可以致电dlopen("libpython2.7.so", RTLD_LAZY | RTLD_GLOBAL);