Unable to get python embedded to work with zip'd library

前端 未结 4 1836
一向
一向 2021-02-09 10:19

I\'m trying to embed python, and provide the dll and a zip of the python libraries and not use any installed python. That is, if a user doesn\'t have python, I want my code to

4条回答
  •  梦谈多话
    2021-02-09 11:04

    I had two issues.

    The not well documented 'Py_NoSiteFlag' fixed the

    ImportError: No module named site
    

    first problem. Next, I had to update paths. In the end, I ended up with the following for initialization:

    Py_NoSiteFlag=1;
    Py_SetProgramName(argv[0]);
    Py_SetPythonHome(".");
    Py_InitializeEx(0);
    PyRun_SimpleString("import sys");
    PyRun_SimpleString("sys.path = ['.','python27.zip','python27.zip/DLLs','python27.zip/Lib','python27.zip/site-packages']");
    

    [edit to address question in comments] Once you have the zip'd file loading, you still need to pass files to the python engine. Here's what I use (argument and file existence checks not included).

    PyObject* PyFileObject = PyFile_FromString(argv[1], "r");
    int res = PyRun_SimpleFile(PyFile_AsFile(PyFileObject), argv[1]);
    

    [/edit]

提交回复
热议问题