LookupError: 嵌入式Python下未知编码 'big5
我正在开发一个R的扩展,它里面嵌入了Python。
现在一切都很顺利,除了Python找不到我需要的编码。每当我做一些与'big5'相关的事情时,它就会不断抛出LookupError的错误。不过,如果我构建一个独立的C++应用程序,Python解释器就能找到编码,并且不再抛出错误。
test.cpp
是一个正常的独立示例:
#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;
}
testr.cpp
是R扩展的代码:
#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')"
时却出现了"LookupError: unknown encoding: big5"的错误。
谢谢
-- 编辑 --
要构建testr.so
,请设置:
export PKG_CXXFLAGS=-I/usr/include/python2.7
export PKG_LIBS=-lpython2.7
1 个回答
0
我注意到这是一个链接问题。
我在嵌入的Python中尝试使用 import encodings.big5
,但出现了 undefined reference
的错误。根据这个链接 http://bugs.python.org/issue4434 的解决方案,对我有效:
在调用
PyInitialize()
之前,我可以先调用dlopen("libpython2.7.so", RTLD_LAZY | RTLD_GLOBAL);