"AttributeError:'module'对象在使用Python.h时没有属性'argv'"

Che*_*ngy 11 c++ python python-embedding python-c-api

搞乱Python.h时我遇到了这个错误:

AttributeError: 'module' object has no attribute 'argv'
Run Code Online (Sandbox Code Playgroud)

C++代码:

#include "stdafx.h"  
#include "C:/Python27/include/Python.h"  
#include <iostream>  

using namespace std;  


int main()  
{
    Py_Initialize();
    PyRun_SimpleString("import sys\nprint sys.argv[0]");
}
Run Code Online (Sandbox Code Playgroud)

在Python中是:

import sys
print sys.argv[0]
Run Code Online (Sandbox Code Playgroud)

我错过了什么?

DSM*_*DSM 17

从概念上讲,sys.argv应该包含调用Python的参数(以及在其下调用的内容).如果像这样调用它应该有什么呢?

您可以加载调用程序的argv进入sys,如果你想:

int main(int argc, char **argv)
{
    Py_Initialize();
    PySys_SetArgv(argc, argv);
    PyRun_SimpleString("import sys\nprint sys.argv");
}
Run Code Online (Sandbox Code Playgroud)

localhost-2:argv $ ./a.out
['./a.out']
localhost-2:argv $ ./a.out arg0 17
['./a.out', 'arg0', '17']
Run Code Online (Sandbox Code Playgroud)

另见Py_SetProgramName.