[英]LookupError: unknown encoding 'big5' under embeded python
我正在構建一個具有嵌入式python的R擴展。
現在一切正常,除了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')"
產生“ LookupError:未知編碼:big5”
謝謝
-編輯-
要構建testr.so
,請設置:
export PKG_CXXFLAGS=-I/usr/include/python2.7
export PKG_LIBS=-lpython2.7
我注意到這是一個鏈接問題。
我試圖在嵌入式python中import encodings.big5
,但是發生undefined reference
的錯誤。 http://bugs.python.org/issue4434中的解決方案對我有用 :
在
PyInitialize()
之前,我可以調用dlopen("libpython2.7.so", RTLD_LAZY | RTLD_GLOBAL);
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.