1
這可能是一個微不足道的,但我有點卡在這裏。Boost :: Python:嵌入和加載Boost :: Python模塊和轉換器
我有以下設置:
entity.cpp/.hpp
:包含我的實體類的定義和實現。entity_wrap.cpp
:我這是我編譯成entity.so
entity_test.cpp
升壓Python包裝的文件:一份測試文件
我想什麼entity_test.cpp
做的是以下幾點:
Py_SetProgramName(argv[0]);
Py_Initialize();
...
Entity* entity = new Entity;
globals["entity"] = entity;
我現在得到以下例外:
TypeError: No to_python (by-value) converter found for C++ type: Entity
這是顯而易見的,因爲我不加載我的類型的轉換定義。我現在試圖加載entity.so
與globals["entity_module"] = import("entity");
,但我遇到了這個例外:
ImportError: No module named entity
我可以從一個Python外殼預期加載模塊。
我現在的問題是:如何加載entity_wrap.cpp
中定義的轉換器?
解決方案
由於eudoxos說,我必須確保我要加載的模塊是sys.path
:
globals["sys"] = import("sys");
exec("sys.path.append('/path/to/my/module')\n"
"import entity", globals);
現在它就像一個魅力。顯然只是使用Py_SetProgramName(argv[0]);
是不夠的。
eudoxos:非常感謝,我幾乎在同一時間得到了類似的解決方案。我會相應地更新我的問題。 – Constantinius