嵌入式python模块导入错误

2024-05-26 22:57:31 发布

您现在位置:Python中文网/ 问答频道 /正文

这是一个用C语言编写的简单的python解释器。我想用.py文件作为用C语言硬编码的引擎的脚本语言——运行这些代码(使用python27.dll/lib)在使用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正在打电话纽玛格斯刺绣) 问题是,当我将可执行文件移植到没有安装python的计算机上时,我得到了一个重要错误:没有名为site的模块。有一些关于设置PYTHONPATH之类的建议,但是id不起作用。我做错什么了?在


Tags: pathpyreturnlibsysstaticnullint

热门问题