Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/cplusplus/139.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
从C+调用Django+;使用Python.h 我需要从C++调用Django函数,并在C++代码中得到它们的输出。 正如您所知,Django是通过调用Manage.py Python脚本来使用的,该脚本的第一个参数是所需的函数,第二个参数是参数_C++_Python_C_Django_Python 2.7 - Fatal编程技术网

从C+调用Django+;使用Python.h 我需要从C++调用Django函数,并在C++代码中得到它们的输出。 正如您所知,Django是通过调用Manage.py Python脚本来使用的,该脚本的第一个参数是所需的函数,第二个参数是参数

从C+调用Django+;使用Python.h 我需要从C++调用Django函数,并在C++代码中得到它们的输出。 正如您所知,Django是通过调用Manage.py Python脚本来使用的,该脚本的第一个参数是所需的函数,第二个参数是参数,c++,python,c,django,python-2.7,C++,Python,C,Django,Python 2.7,现在我有一个名为“test”的测试函数,它返回一个JSON格式的字符串 因此,如果我打电话: python manage.py test 我得到以下信息: {'products': [{'id': 125, 'label': 'Robin'}, {'id': 4, 'label': 'Robinou'}]} 要在cpp中获得此信息,我可以: system(python path.../manage.py test); 并捕获输出。但是我不喜欢它。我也可以使用Boost.

现在我有一个名为“test”的测试函数,它返回一个JSON格式的字符串

因此,如果我打电话:

    python manage.py test
我得到以下信息:

   {'products': [{'id': 125, 'label': 'Robin'}, {'id': 4, 'label': 'Robinou'}]}
要在cpp中获得此信息,我可以:

   system(python path.../manage.py test);
并捕获输出。但是我不喜欢它。我也可以使用Boost.Python。我也不喜欢

出于大小和速度的考虑,我只希望能够在C++/Python.h中实现这一点

这是我正在运行的manage.py:

   import os
   import sys

   sys.path.append('C:\ds')

   if __name__ == "__main__":
       os.environ.setdefault("DJANGO_SETTINGS_MODULE", "frontoffice.settings")

   from django.core.management import execute_from_command_line
   execute_from_command_line(sys.argv)
如果我添加一个
print sys.argv
我会得到以下输出:
['manage.py','test']

所以我设法用一种卑鄙的方式做了这件事:

   bool initPython()
   {
       Py_Initialize();

       int check = 0;

       check = PyRun_SimpleString("import sys");
       check = PyRun_SimpleString("import os");
       check = PyRun_SimpleString("sys.path.append('C:\\ds')");
       check = PyRun_SimpleString("os.environ.setdefault(\"DJANGO_SETTINGS_MODULE\", \"frontoffice.settings\")");
       check = PyRun_SimpleString("from django.core.management import execute_from_command_line");

       if(check == 0) return true;
       else return false;
   }

   std::string execPythonCommand(std::string cmd)
   {
       std::string command = std::string("robin = ['dontreallycarewhatitis',       '").append(cmd).append("']");

       PyRun_SimpleString(command.c_str());
       PyRun_SimpleString("execute_from_command_line(robin)");
   }
没什么特别的。具有跨平台的优势,但捕获输出是一个混乱的过程,因此它可以工作,但这不是我想要的。我希望能够直接从命令行和命令行中获取execute命令的输出

这是我到目前为止的想法。在运行initPython()之后:

现在我什么都试过了。PyObject\u CallObject、CallFunction、CallFunctionObjArgs,使用元组而不是列表,PyErr\u Print()没有给我任何明确的信息。我真的不是Python专家,我正在努力解决这个问题。如何获取pValue来存储测试例程的输出JSON字符串

供参考:

随着时间的推移,我将需要频繁而快速地运行manage.py xxx(其中xxx是一个命令),因此我真的希望使用C/API,而不是执行一些讨厌的stdio捕获

仅供参考,我使用的是VS2012、Windows 8、Python 2.7,后面是PostGRE。
如果您有任何想法,我们将不胜感激。谢谢

> P>我不知道从C++调用Python,但是使用Python动态调用管理命令的方式是通过使用。使用它可能会让事情变得更容易。

理论上,这就是我一直在寻找的。我很震惊花了这么多时间在它上面,却没有找到这个函数<代码>PyRun_SimpleString(“来自django.core.management导入调用_命令”)
check=PyRun\u SimpleString(“调用命令('test')”)工作。但我仍然无法使用PyObjects来完成。有什么想法吗?不管怎样:
PyObject*executeFunc=PyObject\u GetAttrString(executeCmd,“call\u命令”)
PyObject*pValue=PyObject\u调用函数objargs(executeFunc,PyString\u FromString(“test”),NULL)按我的要求工作。谢谢你,丹尼尔!
    PyObject* executeCmd = PyImport_Import(PyString_FromString("django.core.management"));
    PyObject* executeFunc = PyObject_GetAttrString(executeCmd, "execute_from_command_line");

    std::cout << "Imported function." << " isNULL: " << (executeFunc == NULL) << " isExecutable: " << PyCallable_Check((executeFunc)) << std::endl;
    PyObject* pRobin = PyList_New(2);
    PyObject* pString = PyString_FromString("test");
    PyObject* pString0 = PyString_FromString("manage.py");
    PyList_Append(pRobin,pString0);
    PyList_Append(pRobin,pString);

    PyObject* pValue = PyObject_CallObject(executeFunc, pRobin);
    //PyObject* pValue = PyObject_CallFunctionObjArgs(executeFunc, pRobin, NULL); //Doesn't work either
    >>> inspect.getargspec(django.core.management.execute_from_command_line)
    ArgSpec(args=['argv'], varargs=None, keywords=None, defaults=(None,))