-1

I checked already a lot of posts and the subprocess documentation but non of them provided a solution to my problem. At least, i can't find one.

Anyway, here is my problem description: I would like to call a .exe from a .py file. The .exe needs a integer input argument and returns also an integer value, which i would like to use for further calculations in python.

In order to keep things simple, i would like to use a minimun working example of my "problem"-code (see below). If i run this code, then .exe crashes and i don't know why. Maybe i just missed something but i don't know what!? So here is what i did:

c++ code which i use to generate: MyExe.exe

#include <iostream>
using namespace std;
#include <stdlib.h>
#include <string>

int main(int argc, char* argv[])
{

int x = atoi(argv[1]);

return x;

}

My python code:

from subprocess import Popen, PIPE

path = 'Path to my MyExe.exe'

def callmyexe(value):
    p = Popen([path], stdout=PIPE, stdin=PIPE)
    p.stdin.write(bytes(value))
    return p.stdout.read

a = callmyexe(5)
b = a + 1
print(b)

I use MSVC 2015 and Python 3.6.

stevula
  • 11
  • 1
  • 3
  • 2
    Your program is not reading `stdin` (see `cin` for C++), it is reading the command-line argument. You did not supply a command-line argument, so there is no element `argv[1]`, so it crashed - although the exact behaviour is undefined. Your C++ program is not writing to `stdout` (see `cout`), it is returning a number. You seem to be confusing command-line arguments, return statements, and standard streams - they are not related. – cdarke Apr 19 '17 at 18:09
  • Possible duplicate of [Calling an external command in Python](http://stackoverflow.com/questions/89228/calling-an-external-command-in-python) – Christian König Apr 19 '17 at 18:09
  • By the way, `p.stdout.read` just returns a method object, you probably meant `p.stdout.read()` (but that won't work, since your program is not writing to `stdout`). – cdarke Apr 19 '17 at 18:13

1 Answers1

3

You have to use cout for output:

#include <iostream>
using namespace std;
#include <stdlib.h>
#include <string>

int main(int argc, char* argv[])
{
   int x = atoi(argv[1]);
   cout << x;
}

And command line parameters for the input:

from subprocess import check_output

path = 'Path to my MyExe.exe'

def callmyexe(value):
    return int(check_output([path, str(value)]))

a = callmyexe(5)
b = a + 1
print(b)
Daniel
  • 42,087
  • 4
  • 55
  • 81
  • Thank you for this answer! This is very helpfull! – stevula Apr 19 '17 at 19:39
  • Would it alo be possible to do the same task with a dictionary as in- and output? What would be the type of the main()? ?int? – stevula Apr 20 '17 at 14:25
  • You need to encode the dictionary in Python and decode it in C++. For more data, use stdin for transfer – Daniel Apr 20 '17 at 15:22
  • Thank you for your help! :) Hm, honestly i don't know how to do this. Could you maybe give me short example how to do this? For example, lets say i have a dictionary A=dict(one=1). How could i send A to C++ and from C++ back to python? It would be awesome if you could show me how to do this. – stevula Apr 20 '17 at 16:00
  • I thought i could send 'one' and '1' as strings to C++. There i could create a new PyDictObject (e.g PyObject *OutputDict = PyDict_New();) and write my keyword and the corresponding value into it and return this PyDictObject. Could that be a way? – stevula Apr 20 '17 at 16:00