Python 打包成 exe 遇到的一堆坑

首先给大家推荐一下我老师大神的人工智能教学网站。教学不仅零基础,通俗易懂,而且非常风趣幽默,还时不时有内涵黄段子!点这里可以跳转到网站

Python3.6 打包成 exe 遇到的一堆坑

不用pyInstaller和py2exe
一堆坑但都填了
近日用python写了一个小程序,写程序的过程还算顺利,但打包成exe发布给用户时却遇到了一堆坑。

我的配置及用到的包:

  • Python3.6
  • tkinter
  • requests
  • PIL
  • lxml
  • threading

寻找打包工具

网上一搜Python打包成exe,立刻出来一堆pyInstaller和py2exe,可是这两个软件对Python的支持实在差劲,或者说根本不支持Python3.6,反正我就没成功过,一直提示元组索引越界:

IndexError: tuple index out of range

最后网上看到说cx_freeze对python3支持较好,就试了一下。虽然最后成功了,但是还是遇到了一堆坑。

安装

遇到第一个坑就是安装,被这个坑坑得重装了一遍Python。
cx_freeze的安装很简单,只要

pip install cx_freeze

然后进入Python安装的文件夹中的Scripts文件夹

python cxfreeze-postinstall

安装就结束了,然而当我运行

cx_freeze -h

时,却得到提示:

F:\Program不是内部命令

原来我的Python在Program Files文件夹下,所以路径中有空格,就识别不出来了,据说可以通过配置环境变量啥的修复,但是我直接卸了Python重装到根目录了。

打包

搞了半天终于安装好了,直接运行cxfreeze xxx.py,结果依然不行。

KeyError: 'TCL_LIBRARY'

又试了一下运行在Scripts文件夹下的cxfreeze-quickstart.bat
生成了setup.py

from cx_Freeze import setup, Executable

# Dependencies are automatically detected, but it might need
# fine tuning.
buildOptions = dict(packages = [], excludes = [])

import sys
base = 'Win32GUI' if sys.platform=='win32' else None

executables = [
    Executable('tkin.py', base=base)
]

setup(name='12',
      version = '4',
      description = '4',
      options = dict(build_exe = buildOptions),
      executables = executables)

然后放到欲打包的py文件的同文件夹下,运行

Python setup.py build

结果依然不行:

KeyError: 'TCL_LIBRARY'

最后在stackoverflow上找到了解决方案:
在setup.py中加上

import os
os.environ['TCL_LIBRARY']="C:\\Program Files\\Python35\\tcl\\tcl8.6"
os.environ['TK_LIBRARY']="C:\\Program Files\\Python35\\tcl\\tk8.6"
找不到指定模块

具体路径按自己的安装情况写。
结果终于打包成功了。
然而一运行:

ImportError:DLL load failed:找不到指定的模块。
研究了半天才搞明白,应该是缺少tkinter相关的dll,修改setup.py如下:

from cx_Freeze import setup, Executable

# Dependencies are automatically detected, but it might need
# fine tuning.


import sys
import os.path

os.environ['TCL_LIBRARY'] = r'F:\Python36\tcl\tcl8.6'
os.environ['TK_LIBRARY'] = r'F:\Python36\tcl\tk8.6'
base = 'Win32GUI' if sys.platform=='win32' else None
includes = []
include_files = [r"F:\Python36\DLLs\tcl86t.dll",
                 r"F:\Python36\DLLs\tk86t.dll"]
executables = [
    Executable('tkin.py', targetName="seu_jwc_faker.exe",base=base)
]

setup(name='qk',
      version = '1.0',
      description = 'seu qiangke',

      options={"build_exe": {"includes": includes, "include_files": include_files}},
      executables = executables)

然而又出现ModuleNotFoundError:No module named ‘queue’

在include里加上queue
之后又说缺idnadata,也加上
setup.py 如下:

from cx_Freeze import setup, Executable

# Dependencies are automatically detected, but it might need
# fine tuning.


import sys
import os.path

os.environ['TCL_LIBRARY'] = r'F:\Python36\tcl\tcl8.6'
os.environ['TK_LIBRARY'] = r'F:\Python36\tcl\tk8.6'
base = 'Win32GUI' if sys.platform=='win32' else None
includes = [r"queue",r"idna.idnadata"]
include_files = [r"F:\Python36\DLLs\tcl86t.dll",
                 r"F:\Python36\DLLs\tk86t.dll"]
executables = [
    Executable('tkin.py', targetName="seu_jwc_faker.exe",base=base)
]

setup(name='qk',
      version = '1.0',
      description = 'seu qiangke',

      options={"build_exe": {"includes": includes, "include_files": include_files}},
      executables = executables)
这里写图片描述

终于成功了

点这里可以跳转到人工智能网站

发表评论