There is a script that performs file generation. How to run it, passing it the argument? I understood my mistake. I specify, os Windows.

    4 answers 4

    Just call the system () function from code and pass your script and its parameters.

    system("python myscript.py"); 

    The solution is certainly not very beautiful, but it works.

      There is, in my opinion, a more correct solution - the use of the Python / C API ( introduction , documentation ). Your task is solved like this:

       //C code #include <Python.h> #include <cstring> #include <cstdio> int main() { Py_Initialize(); const char* script_path = "./script.py"; char* py_argv[] = {strdup(script_path), strdup("argument")}; PySys_SetArgv(2, py_argv); PyRun_SimpleFileEx(fopen(script_path, "r"), script_path, 1); free(py_argv[0]); free(py_argv[1]); Py_Finalize(); return 0; } 

      =================

       #script.py import sys print sys.argv 

      As a result, the console will display:

       ['./script.py', 'argument'] 

        If the linux operating system, then man exec .

        UPD: Example.

          #include <unistd.h> int main(){ std::string path = "/usr/bin/python"; std::string script = "/home/user/script.py"; std::string arg = "arg"; char* a[] = new char*[2]; a[0] = script.c_str(); a[1] = arg.c_str(); execvp(path.c_str(), a); std::cerr<<"cannot exec"<<endl; return 0; } 

        UPD2: http://msdn.microsoft.com/en-us/library/431x4c1w%28v=vs.71%29.aspx
        Perhaps this is what we need.

        • 2
          In any OS there is a system - Dzhioev
        • one
          Do you know what you will get as a result? The old process image is replaced by a new one. To prevent this from happening and the parent process is preserved, you need to use fork (2). - skegg 2:01 pm

        I agree with all the methods described above, but personally I would prefer to use boost :: python because IMHO is the easiest and most importantly the clean (code) way to make friends with Python .. as for the standard Python C / API, this is In most cases, the Stone Age, which requires quite a lot of time writing two-language spikes ...

        • Si and boost? Incompatible. You mean c ++, right? - gecube
        • Isn't it too redundant to fasten a boost, under Windows, for the sake of a single call? .. - AlexDenisov
        • You mean c ++, right? - yes yes about him and speech .. for the sake of one call? - it is possible and unnecessary, but who knows what will come to mind after the first successful test =) - Alexander Molofeev