V2EX = way to explore
V2EX 是一个关于分享和探索的地方
现在注册
已注册用户请  登录
推荐学习书目
Learn Python the Hard Way
Python Sites
PyPI - Python Package Index
http://diveintopython.org/toc/index.html
Pocoo
值得关注的项目
PyPy
Celery
Jinja2
Read the Docs
gevent
pyenv
virtualenv
Stackless Python
Beautiful Soup
结巴中文分词
Green Unicorn
Sentry
Shovel
Pyflakes
pytest
Python 编程
pep8 Checker
Styles
PEP 8
Google Python Style Guide
Code Style from The Hitchhiker's Guide
goodryb
V2EX  ›  Python

pyinstller 打包 pyqt5 开发的小程序,打开就崩溃

  •  
  •   goodryb · 2017-10-17 14:55:10 +08:00 · 7459 次点击
    这是一个创建于 2624 天前的主题,其中的信息可能已经有所发展或是发生改变。

    开发环境

    Mac 10.13 brew 安装的 python3.6、pyqt5、 pyinstaller 3.3

    开发工具

    PyCharm 2016.3.3

    Build #PY-163.15188.4, built on March 11, 2017

    JRE: 1.8.0_112-release-408-b6 x86_64

    JVM: OpenJDK 64-Bit Server VM by JetBrains s.r.o


    直接运行 python3 main.py 没有问题,各项功能均正常


    打包命令

    $sudo pyinstaller -w -y --onefile main.py
    366 INFO: PyInstaller: 3.3
    366 INFO: Python: 3.6.2
    380 INFO: Platform: Darwin-17.0.0-x86_64-i386-64bit
    381 INFO: wrote /Users/goodryb/PycharmProjects/aliyunUI/src/main.spec
    394 INFO: UPX is available.
    397 INFO: Extending PYTHONPATH with paths
    ['/Users/goodryb/PycharmProjects/aliyunUI/src',
     '/Users/goodryb/PycharmProjects/aliyunUI/src']
    397 INFO: checking Analysis
    397 INFO: Building Analysis because out00-Analysis.toc is non existent
    397 INFO: Initializing module dependency graph...
    400 INFO: Initializing module graph hooks...
    404 INFO: Analyzing base_library.zip ...
    6016 INFO: running Analysis out00-Analysis.toc
    6033 INFO: Caching module hooks...
    6042 INFO: Analyzing /Users/goodryb/PycharmProjects/aliyunUI/src/main.py
    6545 INFO: Processing pre-safe import module hook   urllib3.packages.six.moves
    9180 INFO: Loading module hooks...
    9180 INFO: Loading module hook "hook-requests.py"...
    9183 INFO: Loading module hook "hook-PyQt5.py"...
    9334 INFO: Loading module hook "hook-xml.etree.cElementTree.py"...
    9336 INFO: Loading module hook "hook-encodings.py"...
    9453 INFO: Loading module hook "hook-PyQt5.Qt.py"...
    9464 INFO: Loading module hook "hook-certifi.py"...
    9467 INFO: Loading module hook "hook-PyQt5.QtWidgets.py"...
    9468 INFO: Loading module hook "hook-xml.py"...
    9555 INFO: Loading module hook "hook-pydoc.py"...
    9556 INFO: Loading module hook "hook-PyQt5.QtCore.py"...
    9710 INFO: Loading module hook "hook-PyQt5.QtGui.py"...
    10417 INFO: Loading module hook "hook-PyQt5.QtPrintSupport.py"...
    10574 INFO: Looking for ctypes DLLs
    10594 INFO: Analyzing run-time hooks ...
    10601 INFO: Including run-time hook 'pyi_rth_qt5.py'
    10613 INFO: Looking for dynamic libraries
    11090 INFO: Looking for eggs
    11090 INFO: Using Python library /usr/local/Cellar/python3/3.6.2/Frameworks/Python.framework/Versions/3.6/Python
    11095 INFO: Warnings written to /Users/goodryb/PycharmProjects/aliyunUI/src/build/main/warnmain.txt
    11155 INFO: Graph cross-reference written to /Users/goodryb/PycharmProjects/aliyunUI/src/build/main/xref-main.html
    11182 INFO: checking PYZ
    11182 INFO: Building PYZ because out00-PYZ.toc is non existent
    11183 INFO: Building PYZ (ZlibArchive) /Users/goodryb/PycharmProjects/aliyunUI/src/build/main/out00-PYZ.pyz
    11947 INFO: Building PYZ (ZlibArchive) /Users/goodryb/PycharmProjects/aliyunUI/src/build/main/out00-PYZ.pyz completed successfully.
    11964 INFO: checking PKG
    11964 INFO: Building PKG because out00-PKG.toc is non existent
    11964 INFO: Building PKG (CArchive) out00-PKG.pkg
    23536 INFO: Building PKG (CArchive) out00-PKG.pkg completed successfully.
    23543 INFO: Bootloader /usr/local/lib/python3.6/site-packages/PyInstaller/bootloader/Darwin-64bit/runw
    23543 INFO: checking EXE
    23543 INFO: Building EXE because out00-EXE.toc is non existent
    23543 INFO: Building EXE from out00-EXE.toc
    23544 INFO: Appending archive to EXE /Users/goodryb/PycharmProjects/aliyunUI/src/dist/main
    23635 INFO: Fixing EXE for code signing /Users/goodryb/PycharmProjects/aliyunUI/src/dist/main
    23656 INFO: Building EXE from out00-EXE.toc completed successfully.
    23662 INFO: checking BUNDLE
    23662 INFO: Building BUNDLE because out00-BUNDLE.toc is non existent
    23662 INFO: Building BUNDLE out00-BUNDLE.toc
    23716 INFO: moving BUNDLE data files to Resource directory
    

    告警信息 11095 INFO: Warnings written to /Users/goodryb/PycharmProjects/aliyunUI/src/build/main/warnmain.txt

    $cat warnmain.txt
    missing module named nt - imported by os, ntpath, shutil, /Users/goodryb/PycharmProjects/aliyunUI/src/main.py
    missing module named org - imported by pickle, /Users/goodryb/PycharmProjects/aliyunUI/src/main.py
    excluded module named _frozen_importlib - imported by importlib, importlib.abc, /Users/goodryb/PycharmProjects/aliyunUI/src/main.py
    missing module named _frozen_importlib_external - imported by importlib._bootstrap, importlib, importlib.abc, /Users/goodryb/PycharmProjects/aliyunUI/src/main.py
    missing module named _winreg - imported by platform, requests.utils, /Users/goodryb/PycharmProjects/aliyunUI/src/main.py
    missing module named java - imported by platform, /Users/goodryb/PycharmProjects/aliyunUI/src/main.py
    missing module named 'java.lang' - imported by platform, /Users/goodryb/PycharmProjects/aliyunUI/src/main.py, xml.sax._exceptions
    missing module named vms_lib - imported by platform, /Users/goodryb/PycharmProjects/aliyunUI/src/main.py
    missing module named winreg - imported by platform, mimetypes, urllib.request, requests.utils, /Users/goodryb/PycharmProjects/aliyunUI/src/main.py
    missing module named urllib.proxy_bypass - imported by urllib, requests.compat
    missing module named urllib.unquote_plus - imported by urllib, requests.compat
    missing module named urllib.proxy_bypass_environment - imported by urllib, requests.compat
    missing module named urllib.getproxies - imported by urllib, requests.compat
    missing module named urllib.quote_plus - imported by urllib, requests.compat
    missing module named urllib.urlencode - imported by urllib, requests.compat
    missing module named urllib.getproxies_environment - imported by urllib, requests.compat
    missing module named urllib.unquote - imported by urllib, oss2.compat, requests.compat
    missing module named urllib.quote - imported by urllib, oss2.compat, requests.compat
    missing module named msvcrt - imported by subprocess, getpass, /Users/goodryb/PycharmProjects/aliyunUI/src/main.py
    missing module named _winapi - imported by subprocess, /Users/goodryb/PycharmProjects/aliyunUI/src/main.py
    missing module named _dummy_threading - imported by dummy_threading, /Users/goodryb/PycharmProjects/aliyunUI/src/main.py
    missing module named 'org.python' - imported by copy, /Users/goodryb/PycharmProjects/aliyunUI/src/main.py, xml.sax
    missing module named StringIO - imported by urllib3.packages.six, requests.compat, qiniu.compat
    missing module named urlparse - imported by oss2.compat, requests.compat, qiniu.compat
    missing module named simplejson - imported by oss2.compat, requests.compat, qiniu.compat
    missing module named urllib2 - imported by requests.compat, qiniu.services.cdn.manager
    runtime module named urllib3.packages.six.moves - imported by http.client, urllib3.connectionpool, urllib3.util.response, 'urllib3.packages.six.moves.urllib', urllib3.response
    missing module named backports - imported by urllib3.packages.ssl_match_hostname
    missing module named _abcoll - imported by urllib3.packages.ordered_dict
    missing module named dummy_thread - imported by urllib3.packages.ordered_dict
    missing module named thread - imported by urllib3.packages.ordered_dict
    missing module named "'urllib3.packages.six.moves.urllib'.parse" - imported by urllib3.request, urllib3.poolmanager
    missing module named netbios - imported by uuid
    missing module named win32wnet - imported by uuid
    missing module named Queue - imported by urllib3.connectionpool, oss2.task_queue
    missing module named Cookie - imported by requests.compat
    missing module named cookielib - imported by requests.compat
    missing module named socks - imported by urllib3.contrib.socks
    missing module named 'OpenSSL.crypto' - imported by urllib3.contrib.pyopenssl
    missing module named 'cryptography.x509' - imported by urllib3.contrib.pyopenssl
    missing module named six - imported by urllib3.contrib.pyopenssl
    missing module named 'cryptography.hazmat' - imported by urllib3.contrib.pyopenssl
    missing module named cryptography - imported by urllib3.contrib.pyopenssl
    missing module named OpenSSL - imported by urllib3.contrib.pyopenssl
    missing module named predefined - imported by crcmod
    
    21 条回复    2017-10-19 10:09:57 +08:00
    timetolo
        1
    timetolo  
       2017-10-17 15:32:24 +08:00
    去年我也是这样,然后放弃了
    goodryb
        2
    goodryb  
    OP
       2017-10-17 16:04:44 +08:00
    @timetolo #1 哎!~~,不容易呀,看来只能用脚本跑了
    fyl00
        3
    fyl00  
       2017-10-17 16:08:00 +08:00
    把 console 打开,看报错是啥
    goodryb
        4
    goodryb  
    OP
       2017-10-17 16:11:29 +08:00
    @fyl00 #3 不太明白,打包好之后就是 main.app 了,命令行用 open main.app 打开,停顿一下就运行结束了,没有什么输出,然后弹窗显示崩溃

    http://7xqpyi.com1.z0.glb.clouddn.com/2017_10_17_16_10_35.png
    hand515
        5
    hand515  
       2017-10-17 16:13:02 +08:00
    我试过 ico 打包进去会有问题
    justou
        6
    justou  
       2017-10-17 16:17:28 +08:00
    令人奔溃的奔溃信息没有啊, 试试用--onedir 打包看有错没得, 再加上--debug 打包看看能不能得到错误信息.

    https://pyinstaller.readthedocs.io/en/stable/when-things-go-wrong.html

    以前也折腾过, 在 windows 上也打包过好几个程序呢, pyinstaller 注意事项还是比较多, 研究下它的文档吧
    fyl00
        7
    fyl00  
       2017-10-17 16:24:01 +08:00
    @goodryb #4 打包的时候加 -c

    https://pyinstaller.readthedocs.io/en/stable/usage.html#windows-and-mac-os-x-specific-options

    另外可以用 spec 来配置打包命令,方便点
    goodryb
        8
    goodryb  
    OP
       2017-10-17 16:40:29 +08:00
    @justou #6 目前来看主要是很多模块无法找到,我怀疑是不是和通过 brew 安装 python3 有关系,但直接运行脚本是没问题的
    goodryb
        9
    goodryb  
    OP
       2017-10-17 17:02:21 +08:00
    @fyl00 #7 问题基本明确,warning 中的那些模块找不到,不太清楚 brew 安装的 python3,模块的引用是怎样的,想把模块目录添加进去,发现很分散
    fyl00
        10
    fyl00  
       2017-10-17 17:12:48 +08:00
    @goodryb #9 建议打开 debug 模式和 console,直接看具体报错,那个 warning 有时候不影响,我在 Windows 下打包也有很多 Warning,但可用
    goodryb
        11
    goodryb  
    OP
       2017-10-17 17:44:52 +08:00
    @fyl00 #10

    $sudo pyinstaller -F -c --debug main.py
    125 INFO: PyInstaller: 3.4.dev0+133d18156
    125 INFO: Python: 3.6.2
    136 INFO: Platform: Darwin-17.0.0-x86_64-i386-64bit
    138 INFO: wrote /Users/goodryb/PycharmProjects/aliyunUI/src/main.spec
    149 INFO: UPX is available.
    151 INFO: Extending PYTHONPATH with paths
    ['/Users/goodryb/PycharmProjects/aliyunUI/src',
    '/Users/goodryb/PycharmProjects/aliyunUI/src']
    151 INFO: checking Analysis
    152 INFO: Building Analysis because out00-Analysis.toc is non existent
    152 INFO: Initializing module dependency graph...
    155 INFO: Initializing module graph hooks...
    157 INFO: Analyzing base_library.zip ...
    5699 INFO: running Analysis out00-Analysis.toc
    5712 INFO: Caching module hooks...
    5720 INFO: Analyzing /Users/goodryb/PycharmProjects/aliyunUI/src/main.py
    6276 INFO: Processing pre-safe import module hook urllib3.packages.six.moves
    9115 INFO: Loading module hooks...
    9115 INFO: Loading module hook "hook-requests.py"...
    9117 INFO: Loading module hook "hook-PyQt5.py"...
    9280 INFO: Loading module hook "hook-xml.etree.cElementTree.py"...
    9282 INFO: Loading module hook "hook-encodings.py"...
    9407 INFO: Loading module hook "hook-PyQt5.Qt.py"...
    9414 INFO: Loading module hook "hook-certifi.py"...
    9417 INFO: Loading module hook "hook-PyQt5.QtWidgets.py"...
    9419 INFO: Loading module hook "hook-xml.py"...
    9519 INFO: Loading module hook "hook-pydoc.py"...
    9520 INFO: Loading module hook "hook-PyQt5.QtCore.py"...
    9677 INFO: Loading module hook "hook-PyQt5.QtGui.py"...
    10381 INFO: Loading module hook "hook-PyQt5.QtPrintSupport.py"...
    10537 INFO: Looking for ctypes DLLs
    10547 INFO: Analyzing run-time hooks ...
    10552 INFO: Including run-time hook 'pyi_rth_qt5.py'
    10572 INFO: Looking for dynamic libraries
    11050 INFO: Looking for eggs
    11050 INFO: Using Python library /usr/local/Cellar/python3/3.6.2/Frameworks/Python.framework/Versions/3.6/Python
    11056 INFO: Warnings written to /Users/goodryb/PycharmProjects/aliyunUI/src/build/main/warnmain.txt
    11119 INFO: Graph cross-reference written to /Users/goodryb/PycharmProjects/aliyunUI/src/build/main/xref-main.html
    11144 INFO: checking PYZ
    11145 INFO: Building PYZ because out00-PYZ.toc is non existent
    11145 INFO: Building PYZ (ZlibArchive) /Users/goodryb/PycharmProjects/aliyunUI/src/build/main/out00-PYZ.pyz
    11882 INFO: Building PYZ (ZlibArchive) /Users/goodryb/PycharmProjects/aliyunUI/src/build/main/out00-PYZ.pyz completed successfully.
    11898 INFO: checking PKG
    11899 INFO: Building PKG because out00-PKG.toc is non existent
    11899 INFO: Building PKG (CArchive) out00-PKG.pkg
    23726 INFO: Building PKG (CArchive) out00-PKG.pkg completed successfully.
    23734 INFO: Bootloader /usr/local/lib/python3.6/site-packages/PyInstaller/bootloader/Darwin-64bit/run_d
    23734 INFO: checking EXE
    23734 INFO: Building EXE because out00-EXE.toc is non existent
    23734 INFO: Building EXE from out00-EXE.toc
    23735 INFO: Appending archive to EXE /Users/goodryb/PycharmProjects/aliyunUI/src/dist/main
    23793 INFO: Fixing EXE for code signing /Users/goodryb/PycharmProjects/aliyunUI/src/dist/main
    23804 INFO: Building EXE from out00-EXE.toc completed successfully.


    单文件打开的时候

    $./main
    [97057] PyInstaller Bootloader 3.x
    [97057] LOADER: executable is /Users/goodryb/PycharmProjects/aliyunUI/src/dist/main
    [97057] LOADER: homepath is /Users/goodryb/PycharmProjects/aliyunUI/src/dist
    [97057] LOADER: _MEIPASS2 is NULL
    [97057] LOADER: archivename is /Users/goodryb/PycharmProjects/aliyunUI/src/dist/main
    [97057] LOADER: Extracting binaries
    [97057] LOADER: Executing self as child
    [97057] LOADER: set _MEIPASS2 to /var/folders/ks/5f8mgk0s18v3vvtm6fcwjrvr0000gn/T/_MEIFM9gJa
    [97058] PyInstaller Bootloader 3.x
    [97058] LOADER: executable is /Users/goodryb/PycharmProjects/aliyunUI/src/dist/main
    [97058] LOADER: homepath is /Users/goodryb/PycharmProjects/aliyunUI/src/dist
    [97058] LOADER: _MEIPASS2 is /var/folders/ks/5f8mgk0s18v3vvtm6fcwjrvr0000gn/T/_MEIFM9gJa
    [97058] LOADER: archivename is /Users/goodryb/PycharmProjects/aliyunUI/src/dist/main
    [97058] LOADER: Already in the child - running user's code.
    [97058] LOADER: Python library: /var/folders/ks/5f8mgk0s18v3vvtm6fcwjrvr0000gn/T/_MEIFM9gJa/Python
    [97058] LOADER: Loaded functions from Python library.
    [97058] LOADER: Manipulating environment (sys.path, sys.prefix)
    [97058] LOADER: Pre-init sys.path is /var/folders/ks/5f8mgk0s18v3vvtm6fcwjrvr0000gn/T/_MEIFM9gJa/base_library.zip:/var/folders/ks/5f8mgk0s18v3vvtm6fcwjrvr0000gn/T/_MEIFM9gJa
    [97058] LOADER: sys.prefix is /var/folders/ks/5f8mgk0s18v3vvtm6fcwjrvr0000gn/T/_MEIFM9gJa
    [97058] LOADER: Setting runtime options
    [97058] LOADER: Initializing python
    [97058] LOADER: Overriding Python's sys.path
    [97058] LOADER: Post-init sys.path is /var/folders/ks/5f8mgk0s18v3vvtm6fcwjrvr0000gn/T/_MEIFM9gJa/base_library.zip:/var/folders/ks/5f8mgk0s18v3vvtm6fcwjrvr0000gn/T/_MEIFM9gJa
    [97058] LOADER: Setting sys.argv
    [97058] LOADER: setting sys._MEIPASS
    [97058] LOADER: importing modules from CArchive
    [97058] LOADER: extracted struct
    [97058] LOADER: callfunction returned...
    [97058] LOADER: extracted pyimod01_os_path
    [97058] LOADER: callfunction returned...
    [97058] LOADER: extracted pyimod02_archive
    [97058] LOADER: callfunction returned...
    [97058] LOADER: extracted pyimod03_importers
    [97058] LOADER: callfunction returned...
    [97058] LOADER: Installing PYZ archive with Python modules.
    [97058] LOADER: PYZ archive: out00-PYZ.pyz
    [97058] LOADER: Running pyiboot01_bootstrap.py
    [97058] LOADER: Running pyi_rth_qt5.py
    [97058] LOADER: Running main.py
    objc[97058]: Class RunLoopModeTracker is implemented in both /var/folders/ks/5f8mgk0s18v3vvtm6fcwjrvr0000gn/T/_MEIFM9gJa/QtCore (0x10de2a010) and /usr/local/Cellar/qt/5.9.1/lib/QtCore.framework/Versions/5/QtCore (0x112cdf010). One of the two will be used. Which one is undefined.
    objc[97058]: Class NotificationReceiver is implemented in both /var/folders/ks/5f8mgk0s18v3vvtm6fcwjrvr0000gn/T/_MEIFM9gJa/QtWidgets (0x10f153218) and /usr/local/Cellar/qt/5.9.1/lib/QtWidgets.framework/Versions/5/QtWidgets (0x1131f7218). One of the two will be used. Which one is undefined.
    objc[97058]: Class QCocoaPageLayoutDelegate is implemented in both /var/folders/ks/5f8mgk0s18v3vvtm6fcwjrvr0000gn/T/_MEIFM9gJa/QtPrintSupport (0x10f372ef0) and /usr/local/Cellar/qt/5.9.1/lib/QtPrintSupport.framework/Versions/5/QtPrintSupport (0x10ff4fef0). One of the two will be used. Which one is undefined.
    objc[97058]: Class QCocoaPrintPanelDelegate is implemented in both /var/folders/ks/5f8mgk0s18v3vvtm6fcwjrvr0000gn/T/_MEIFM9gJa/QtPrintSupport (0x10f372f40) and /usr/local/Cellar/qt/5.9.1/lib/QtPrintSupport.framework/Versions/5/QtPrintSupport (0x10ff4ff40). One of the two will be used. Which one is undefined.
    QObject::moveToThread: Current thread (0x7ffd8270ba70) is not the object's thread (0x7ffd824e26a0).
    Cannot move to target thread (0x7ffd8270ba70)

    You might be loading two sets of Qt binaries into the same process. Check that all plugins are compiled against the right Qt binaries. Export DYLD_PRINT_LIBRARIES=1 and check that only one set of binaries are being loaded.
    This application failed to start because it could not find or load the Qt platform plugin "cocoa"
    in "".

    Available platform plugins are: cocoa, minimal, offscreen.

    Reinstalling the application may fix this problem.
    [1] 97057 abort ./main
    lrxiao
        12
    lrxiao  
       2017-10-17 22:53:24 +08:00
    missing 很多 module 没关系的
    你用 terminal 运行看报什么错。。特殊的包比如说 matplotlib canvas 需要特殊支持
    我做过 PyQt4 的没问题
    lrxiao
        13
    lrxiao  
       2017-10-17 22:54:35 +08:00
    这种算是疑难杂症。。去 SO 和 pyinstaller issue 下搜搜
    leavic
        14
    leavic  
       2017-10-18 06:43:56 +08:00 via iPhone
    pyinstaller 现在官方 release 版支持 python3.6 了吗?
    goodryb
        15
    goodryb  
    OP
       2017-10-18 09:55:29 +08:00
    @lrxiao #12 原文件命令行运行没有任何问题,打包之后运行报错如上

    This application failed to start because it could not find or load the Qt platform plugin "cocoa"
    in "".

    我去 issue 搜了,似乎没有类似的问题,我提了一个,但是作者并没有正面回复
    https://github.com/pyinstaller/pyinstaller/issues/2924


    @leavic #14 3.3 版本应该是支持了,我使用的是 3.4 dev 版本
    BadReese
        16
    BadReese  
       2017-10-18 15:46:03 +08:00
    @goodryb 不知道怎样把其他库也当作依赖一起打包进去。我整了个包含 MitmProxy 这个库的,但是似乎打包不进去,因此运行 exe 的环境需要有这个库,如果没这个库,exe 不能正常跑。你是否知道怎样一起打包呢?
    goodryb
        17
    goodryb  
    OP
       2017-10-18 16:00:21 +08:00
    @BadReese #16 我也不清楚,我理解应该会自动把所有依赖的包都打进去才对,否则缺少依赖就报错了。

    截至目前,我的问题还没有解决
    suntxy
        18
    suntxy  
       2017-10-18 19:02:55 +08:00
    pyinstaller 各种 bug 不稳定。。生产环境还是别用了
    goodryb
        19
    goodryb  
    OP
       2017-10-18 21:00:27 +08:00
    @suntxy #18 打包方便分发,现在只能自己用了
    BadReese
        20
    BadReese  
       2017-10-19 08:04:03 +08:00 via iPhone
    @goodryb 那真是很麻烦了 得配置一堆环境
    goodryb
        21
    goodryb  
    OP
       2017-10-19 10:09:57 +08:00
    @BadReese #20 我已经下载了 xcode,学习下 swift,至少这个打包应该问题不大
    关于   ·   帮助文档   ·   博客   ·   API   ·   FAQ   ·   实用小工具   ·   5836 人在线   最高记录 6679   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 26ms · UTC 02:28 · PVG 10:28 · LAX 18:28 · JFK 21:28
    Developed with CodeLauncher
    ♥ Do have faith in what you're doing.