3

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 syntax

Current thread 0x00007ffff7fd1700 (most recent call first): Aborted (core dumped)

There are duplicates of this question:

Fatal Python error: Py_Initialize: Unable to get the locale encoding ... SyntaxError: invalid syntax Aborted (core dumped)

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.

  1. 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
  2. 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 ?

1 Answer 1

2

I got a similar problem in a different context (when calling a python3 script from the interactive environment of the VisIt plot program). The reason this happens is that apparently, the environment variables (in particular, PATH) are set to local values that disable the system-wide install of Python3.

You can get around this by running Python 3 in a clean-slate env. Should look something like this:

ostringstream command;
command<<"cd ; env -i bash -l -c 'python3 piap.py "<<u<<" "<<rho<<"'";
if(!(in = popen(command.str().c_str(), "r"))){
    cout<<"some problem with the subprocess"<<endl;
}

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .