SyntaxError when using cx_freeze on PyQt app

2019-04-09 16:19发布

问题:

This is quite annoying problem that occurs when trying to build .exe file from Python 3 script using PyQt4. I think it is connected with using uic module for dynamic loading of .ui files. cx_freeze returns:

  File "E:\Python32_32\lib\site-packages\cx_Freeze\finder.py", line 366, in _LoadModule
    module.code = compile(codeString, path, "exec")
  File "E:\Python32_32\lib\site-packages\PyQt4\uic\port_v2\load_plugin.py", line 41
    except Exception, e:
                    ^
SyntaxError: invalid syntax

I have solved this problem some time ago but I have recently encountered it again. Finding solution on the web isn't easy so I decided to post Q&A on SO. Hope it will be helpful not only for me :-).

回答1:

Problem lies in fact that cx_freeze tries to use uic submodule for Python 2, not 3 and encounters Py3-incompatible syntax in one of files. Solution is quite simple:

Find uic directory, it should be located in your_python_dir\Lib\site-packages\PyQt4\uic. There are two directories there: port_v2 and port_v3. Rename the first one, for example to port_v2_. I think similar problem can occur while using Python2-rename v3 then. Anyway, this will cause cx_freeze to find proper version of uic and the script should build correctly.