2013-02-24 224 views
-1

下面是C.簡單Python解釋器的最簡單的我想是使用.py文件作爲腳本語言發動機用C硬編碼 - 運行此代碼(與python27.dll/lib目錄)中運行用python在機器上很好。嵌入式Python模塊導入錯誤

#pragma comment(lib,"python27.lib") 
#include <Python27/Python.h> 

static PyObject* emb_numargs(PyObject *self, PyObject *args) 
{ 
    if(!PyArg_ParseTuple(args, ":numargs")) 
     return NULL; 
    return Py_BuildValue("i", 1); 
} 

static PyMethodDef EmbMethods[] = { 
    {"numargs", emb_numargs, METH_VARARGS, 
    "Return 1 you dumb person."}, 
    {NULL, NULL, 0, NULL} 
}; 




    int main(int argc, char *argv[]) 
{ 
    FILE *fp; 
    Py_SetProgramName(argv[0]); /* optional but recommended */ 
    Py_Initialize(); 



    { 
    int i; 
     PyObject* sys = PyImport_ImportModule("sys"); 
    PyObject* path = PyObject_GetAttrString(sys, "path"); 

    // Add current project path to sys.path 
    PyList_Append(path, PyString_FromString(".")); 
    for (i = 0; i < PyList_Size(path); i++) 
    { 
    PyString_AsString(PyList_GetItem(path, i)); 
    } 
    Py_DECREF(sys); 
    } 

    Py_InitModule("emb", EmbMethods); 


    fp = fopen("a.py", "r"); 
    PyRun_SimpleFile(fp, "a.py"); 

    Py_Finalize(); 

    system("pause"); 
    return 0; 
} 

(a.py只是調用emb.numargs) 問題是,當我端口可執行文件沒有安裝Python的一臺電腦,我得到的導入錯誤:沒有模塊命名的網站。有關於設置PYTHONPATH和類似的建議,但ID不起作用。我究竟做錯了什麼?

回答

0

好的,我發現問題在於必須從python主文件夾中分發Lib文件夾以及可執行文件