在使用cx_freeze和Py2App时,我无法从基于python Qt的程序为Mac制作独立应用程序。 (请注意,我使用cx_freeze在Windows上成功完成了它。)
实际上, cx_freeze提供的'PyQt4'示例在Mac上无法正常运行。有人能够在Mac上“构建”并运行此示例吗?!我在论坛上找不到关于这个看似基本问题的任何信息。
确实,让我们先建立:
~$ cd cx_Freeze-4.3.3/cx_Freeze/samples/PyQt4/
PyQt4$ ls
PyQt4app.py setup.py
PyQt4$ python setup.py build
running build
running build_exe
creating directory build/exe.macosx-10.5-x86_64-2.7
copying //anaconda/lib/python2.7/site-packages/cx_Freeze-4.3.3-py2.7-
macosx-10.5-x86_64.egg/cx_Freeze/bases/Console -> build/exe.macosx-10.5-x86_64-2.7/PyQt4app
TD bug fix: adding //anaconda/lib to relative path libpython2.7.dylib!
copying //anaconda/lib/libpython2.7.dylib -> build/exe.macosx-10.5-x86_64-2.7/libpython2.7.dylib
TD bug fix: adding //anaconda/lib to relative path libpython2.7.dylib!
writing zip file build/exe.macosx-10.5-x86_64-2.7/library.zip
…
copying //anaconda/lib/python2.7/lib-dynload/zlib.so -> build/exe.macosx-10.5-x86_64-2.7/zlib.so
PyQt4$
注意“TD bug fix ...”这一行:为了让上面的运行没有“找不到文件”错误,我必须在cx_freeze文件_GetDependentFiles
中为函数freezer.py
添加一个更正:
for i in range(len(dependentFiles)):
filei = dependentFiles[i]
if not os.path.isabs(filei):
print 'TD bug fix: adding ' + sys.prefix + '/lib to relative path ' + filei + '!'
dependentFiles[i] = os.path.join(sys.prefix,'lib',filei)
现在,当我尝试运行创建的独立程序时,我得到:
PyQt4$ ls
PyQt4app.py build setup.py
PyQt4$ cd build/exe.macosx-10.5-x86_64-2.7/
exe.macosx-10.5-x86_64-2.7$ ls
PyQt4.QtCore.so _codecs_iso2022.so _struct.so libQtCore.dylib libz.1.dylib
PyQt4.QtGui.so _codecs_jp.so binascii.so libQtGui.4.dylib sip.so
PyQt4app _codecs_kr.so bz2.so libQtGui.dylib unicodedata.so
_codecs_cn.so _codecs_tw.so itertools.so libpython2.7.dylib zlib.so
_codecs_hk.so _multibytecodec.so libQtCore.4.dylib library.zip
exe.macosx-10.5-x86_64-2.7$ ./PyQt4app
Traceback (most recent call last):
File "//anaconda/lib/python2.7/site-packages/cx_Freeze-4.3.3-py2.7-macosx-10.5-x86_64.egg/cx_Freeze/initscripts/Console.py", line 27, in <module>
exec(code, m.__dict__)
File "PyQt4app.py", line 5, in <module>
File "ExtensionLoader_PyQt4_QtGui.py", line 22, in <module>
File "ExtensionLoader_PyQt4_QtGui.py", line 14, in __bootstrap__
ImportError: dlopen(/Users/thomasdeneux/cx_Freeze-4.3.3/cx_Freeze/samples/PyQt4/build/exe.macosx-10.5-x86_64-2.7/PyQt4.QtGui.so, 2): Library not loaded: @loader_path/../../../libQtGui.4.dylib
Referenced from: /Users/thomasdeneux/cx_Freeze-4.3.3/cx_Freeze/samples/PyQt4/build/exe.macosx-10.5-x86_64-2.7/PyQt4.QtGui.so
Reason: image not found
exe.macosx-10.5-x86_64-2.7$
碰巧找不到某些Qt库,因为它们是用完整的相对路径引用的,而在发行版中,所有库都被复制到同一个文件夹中。
我已经转移到Py2App,希望另一个包不会遇到这样的问题。我现在没有显示所有细节,只有最重要的细节在尝试构建时(这次我不需要修改代码来运行构建)并运行另一个简单的基于Qt的helloworld程序:
test$ py2applet --make-setup helloworld.py
Wrote setup.py
test$ python setup.py py2app
running py2app
creating /Users/thomasdeneux/Documents/spyderworkspace/securebox/python/build/bdist.macosx-10.5-x86_64
...
Done!
test$ ./dist/helloworld.app/Contents/MacOS/helloworld
Traceback (most recent call last):
File "/Users/thomasdeneux/Documents/spyderworkspace/securebox/python/dist/helloworld .app/Contents/Resources/__boot__.py", line 351, in <module>
_run()
File "/Users/thomasdeneux/Documents/spyderworkspace/securebox/python/dist/helloworld .app/Contents/Resources/__boot__.py", line 336, in _run
exec(compile(source, path, 'exec'), globals(), globals())
File "/Users/thomasdeneux/Documents/spyderworkspace/securebox/python/dist/helloworld .app/Contents/Resources/helloworld.py", line 4, in <module>
from PyQt4.QtGui import *
File "PyQt4/QtGui.pyc", line 14, in <module>
File "PyQt4/QtGui.pyc", line 10, in __load
ImportError: dlopen(/Users/thomasdeneux/Documents/spyderworkspace/securebox/python/dist/helloworld.app/Contents/Resources/lib/python2.7/lib-dynload/PyQt4/QtGui.so, 2): Library not loaded: @loader_path/../../../libQtGui.4.dylib
Referenced from: /Users/thomasdeneux/Documents/spyderworkspace/securebox/python/dist/helloworld.app/Contents/Resources/lib/python2.7/lib-dynload/PyQt4/QtGui.so
Reason: image not found
2015-05-16 20:40:41.546 helloworld[983:707] helloworld Error
test$
出现了同样的问题!
感谢任何人能够提供帮助,无论是直接解决问题,还是建议从python / Qt项目为Mac创建独立程序的其他策略。