I'm trying to execute a python script from C++ by calling popen().
The important code looks like this:
ostringstream command;
command<<"cd ; python3 piap.py "<<u<<" "<<rho;
if(!(in = popen(command.str().c_str(), "r"))){
cout<<"some problem with the subprocess"<<endl;
}
I also tried calling python3.5
in popen
but that doesn't change anything.
You can find the whole cpp file here:http://pastebin.com/KeXsgzjx But the rest is just problem-specific boilerplate.
The code compiles just fine, but during runtime, it produces this error message:
Fatal Python error: Py_Initialize: Unable to get the locale encoding
File "/home/lhk/programs/clion-2016.1.2/bin/gdb/lib/python2.7/encodings/init.py", line 123 raise CodecRegistryError,\ ^ SyntaxError: invalid syntaxCurrent thread 0x00007ffff7fd1700 (most recent call first): Aborted (core dumped)
There are duplicates of this question:
Bash tries to run python on unknown command and fails in Py_Initialize
Unfortunately the solutions don't work for me:
- I haven't set pythonpath
- My python script specifies the exact version with
#!/usr/bin/python3.5
- My
/usr/lib/command-not-found
explicitly calls/usr/bin/python3
And there's even more to this bug.
- It appears to be related to my IDE, Clion. You can see that in the error message. Somehow the python interpreter tries to load an init script from the clion distribution
- It only appears during debugging. When ran in release mode, the popen() call works just fine.
I checked which python version was configured as the default in Clion, it's 3.5, and the correct interpreter, too.
I'm at a loss, why is the wrong interpreter chosen ?