Pyinstller打包exe报错解决

前言

  • 用python写了个自动化工具,为了方便没有python环境的同事使用,故准备将其打包成exe。
  • 由于本人使用的是虚拟环境,原来在默认的虚拟环境下没有pyinstaller,且写代码的电脑没有网络,因此只能本地安装包。

安装

  • 之前的项目是python2.7,安装pyinstaller时,需要安装各种依赖库,如下:
    - altgraph
    - dis3
    - future
    - macholib
    - pefile
    - pywin32-ctypes
  • 其中dis3和future都是因为python2的缘故,需要导入部分python3的特征。而通过阅读pyinstaller文档得知,pyinstaller3.4已经完全停止对pywin32的依赖,转而依赖pywin32-ctype。

问题

成功打包后,文件报错
Traceback (most recent call last): File “PyInstaller/loader/rthooks/pyi_rth__tkinter.py”, line 28, in FileNotFoundError: Tcl data directory “/var/folders/nl/2wxh3z313l129zpzq2f8ww5m0000gn/T/_MEILUmJRc/tcl” not found. [2195] Failed to execute script pyi_rth__tkinter

一开始以为是虚拟环境的问题,排查了好久。
最后通过查阅各种外网得知,原因如下:

The Error Message basically refers to a file that cannot be found. In most cases, this file may not be necessary to run your application.
The only way to know it is to refer to the file pyi_rth_tkinter.py

The file pyi_rth_tkinter.py can be found under the venv folder of your
project.

venv/lib/python3.7/site-packages/PyInstaller/loader/rthooks/pyi_rth_tkinter.py

You will find a simple IF Statement that returns an error if a
specific file is not found in the tk or tcl directory. You can comment
the IF Statement as provided below and try running your PyInstaller
code once again

注释掉这几行代码(很神奇的解决方式。)

#if not os.path.isdir(tcldir):
    #raise FileNotFoundError('Tcl data directory "%s" not found.' % (tcldir))
#if not os.path.isdir(tkdir):
    #raise FileNotFoundError('Tk data directory "%s" not found.' % (tkdir))

最终生成的exe成功执行~

你可能感兴趣的:(python,python,pyinstaller,bug)