4

I'm Python new comer. I tried to call a python script from c++ like this (in Raspberry Pi).

std::string pythonCommand = "python Callee.py \""+a+"\" "+b;
int res = system(pythonCommand.c_str());

after running I got this.

python: can't open file 'Callee.py': [Errno 2] No such file or directory

But I can run Callee.py with command line successfully and both file stored in same directory.

What was I missing ?

VMRuiz
  • 1,931
  • 12
  • 20
3ORZ
  • 79
  • 1
  • 3

2 Answers2

3

You probably are running the python interpreter (and your python Callee.py command) in some strange directory (i.e. in some other directory than what you are expecting).

You could use getcwd(3) before your call to system(3) to find out your current working directory.

You might use the chdir(2) system call (before calling system) to change the directory to something appropriate. See perhaps this.

I recommend also reading Advanced Linux Programming

Read also about Extending and Embedding the Python Interpreter; but if you need to embed some interpreter consider also Guile & Lua.

Basile Starynkevitch
  • 223,805
  • 18
  • 296
  • 547
1

you can try something like this

system("/work/test/pythonscript.sh")

and define inside this script how your python script is executed/called.

This way you dont trip over format errors (c_string() and "\r" or OS-dependent line endings)

  • Will only work if that `/work/test/pythonscript.sh` file is executable and starts with appropriate shebang – Basile Starynkevitch May 24 '17 at 10:39
  • You are right. But because OP mentioned a `Callee.py` script, I think this problem has already been concerned. It also works only if you run it on a linux machine, for other OS you need to edit the path correctly – John.A.Myer May 24 '17 at 11:19