I run the script like this:

QProcess proс; proc.setProgram("cam\\python-3.5.2\\python.exe"); proc.setArguments(QStringList() << "cam\\Program\\rotronic-rcvr\\json.py"); QProcessEnvironment env = QProcessEnvironment::systemEnvironment(); proc.setProcessEnvironment(env); proc.setStandardErrorFile("error.log"); proc.setStandardOutputFile("output.log"); proc.setWorkingDirectory("cam\\python-3.5.2"); proc.start(); proc.waitForStarted(1000); 

After running python, it crashes right away. In the standard error stream displays the following:

Fatal Python error: unable to load the file system codec ImportError: No module named 'encodings'

Current thread 0x00004ed8 (most recent call first):

Moreover, if you run the script from the command line, for example, like this:

cam \ python-3.5.2 \ python.exe cam \ Program \ rotronic-rcvr \ json.py

then it runs without problems. What could be the problem?

  • one
    do not use module names that conflict with the standard library - rename json.py - jfs

2 answers 2

It was decided so. The env list somehow got the PYTHONHOME variable, in which some kind of left path was written. After removing it all worked.

  • Select this answer as correct - FeroxTL

Maybe PYTHONPATH is not set? Check this variable in the global environment.

encodings is one of the standard python modules, maybe due to the lack of paths in the PATH, it can not find it

There is a bug on this topic http://bugs.python.org/issue11288

upd

I unfortunately do not know c ++, but I am prompted by the decision of the form

 Py_SetPythonHome(L"C:\\Path\\To\\My\\Python\\Installation") 
  • one
    The link discussion offers a solution exactly the opposite: the %PYTHONPATH% environment variable must be cleaned so that it does not contain the path from Python 2 (this environment variable is empty during normal operation). - jfs
  • Tomorrow I'll check. - maestro
  • @jfs I don’t know very well how python works on windows, but I’ve heard that it needs to be registered in PATH for normal operation. The link really had a version conflict that the author has - all that remains is to guess - FeroxTL
  • 1- PATH and PYTHONPATH are different things. 2- here with ++ runs python as an external process (analog: subprocess.call([sys.executable, r'..\json.py'], stdout=open('output.log'), stderr=open('error.log'), cwd=os.path.dirname(sys.executable), timeout=1) ), and the Py_SetPythonHome() call makes sense only if you try to embed the Python interpreter inside the process (although it is worth checking that the PYTHONHOME variable environment is not installed. - jfs
  • @jfs is strange then. I did not realize that not from this program python runs fine. But I still think that the environment is somehow changing. Most likely these are some subtleties of QProcessEnvironment, I do not know. Delete your answer if it doesn't help - FeroxTL