Salut tous le monde,
Je suis débutant en python/c++ api,
mon script est:

Code : Sélectionner tout - Visualiser dans une fenêtre à part
1
2
3
4
5
6
7
8
9
10
11
12
13
 
#script.py
 
import sys
 
# my module directory
sys.path.append("C:\Python25\DLLs")
 
#module is created by swig generator "_SwigModule.pyd
import SwigModule
fo = open("foo.txt", "wb")
fo.write( "test");
fo.close()
Par la suite , je veux exécuter ce script par ce code :

Code : Sélectionner tout - Visualiser dans une fenêtre à part
1
2
3
4
5
6
7
8
9
10
11
 
 Py_Initialize();
 string *pythonScript = readPythonScript( /*path of my script (script.py)*/path);
 
if( pythonScript != NULL )
{
    PyRun_SimpleString(pythonScript->c_str());
 
    delete pythonScript;
}
 Py_Finalize();
La fonction readPythonScript est lire notre script à partir le path et l'affecter dans un string.
Leur définition est :

Code : Sélectionner tout - Visualiser dans une fenêtre à part
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
 
 string *readPythonScript( string fileName )
{
ifstream pythonFile;
 
pythonFile.open( fileName.c_str() );
 
if ( !pythonFile.is_open() ) 
{
    cout << "Cannot open Python script file, \"" << fileName << "\"!" << endl;
    return NULL;
}
else
{
    // Get the length of the file
    pythonFile.seekg( 0, ios::end );
    int nLength = pythonFile.tellg();
    pythonFile.seekg( 0, ios::beg );
 
    // Allocate  a char buffer for the read.
    char *buffer = new char[nLength];
    memset( buffer, 0, nLength );
 
    // read data as a block:
    pythonFile.read( buffer, nLength );
 
    string *scriptString = new string;
    scriptString->assign( buffer );
 
    delete [] buffer;
    pythonFile.close();
 
    return scriptString;
  }
  }
Si je commente "import SwigModule", c'est bon,le fichier foo.txt est creé.
Sinon il peut pas être créé , donc je pense qu'il y a un problème entre le module swig et Python/c++ .
Svp , aidez moi c'est urgent !