文件__init__.py
爲什麼或如何導致python解釋器搜索模塊的子目錄 - 以及爲什麼解釋器從C++調用時不遵守這個約定?__init__.py在python下降dirtree,但不是從C++;導致「導入matplotlib」錯誤
這是我知道的:
在我的程序中使用strace的,我可以看到,正在爲雙方互動情況和 C++調用執行相同的python2.5 解釋。
在這兩種情況下,PYTHONPATH都會指示 搜索引入的模塊(matplotlib)。這顯示爲 系列open()調用,從當前工作目錄開始, 擴展到PYTHONPATH(這裏是/opt/epd/lib/python2.5/site-packages
),最後是 ,最後進入子工作目錄。
完整披露的是,我使用的是「Enthought」分佈,不得不 地方__init__.py
文件中的site-packages
目錄,並把 site-packages
目錄中的PYTHONPATH創建工作情況。
代碼如下。似乎我可能需要撥打電話配置 python解釋器以查找__init__
和/或遞歸,以便查找請求的軟件包 。如果是這樣,怎麼辦?
PyObject* main_module, * global_dict, * expression, *args, *spec;
Py_Initialize();
char* script = "abc.py";
PySys_SetArgv(1, &script);
//Open the file containing the python modules we want to run
FILE* file_1 = fopen("abc.py", "r");
if (file_1 == 0) fprintf(stdout, "ERROR: File not opened");
//Loads the python file into the interpreter
PyRun_SimpleFile(file_1, "abc.py");
//Creates a python object that contains references to the functions and classes in abc.py
main_module = PyImport_AddModule("__main__");
global_dict = PyModule_GetDict(main_module);
expression = PyDict_GetItemString(global_dict, "view_gui");
spec = PyObject_CallObject(expression, NULL);
PyObject_CallMethod(spec, "shutdown", NULL);
Py_Finalize();
return NULL;
當python腳本從C++調用,搜索似乎 停止在文件/opt/epd/lib/python2.5/site-packages/matplotlib
(或它的變種 ,matplotlib.so等)都沒有發現。
請注意,我可以增加PYTHONPATH以包含 matplotlib(和其他所需包)的確切位置以獲得更遠;不過,我似乎無法看到 包含導入matplotlib.cbook
的路徑。