传统弱校HFUT的蒟蒻,真相只有一个

python 使用cx-freeze打包程序

python环境 3.6.5      win7   linux环境同理

先尝试了PyInstaller   ,打包时一直提示 no module named gtk   而gtk 又依赖pygobject ,尝试安装几次之后失败放弃

 

改用cx-freeze

首先写一个 setup.py

1.py     2.py     是自己要打包的程序名字

注意里面的四个绝对路径改成真实的路径,否则会爆各种奇怪的错误,运行   python setup.py install

执行exe 比如下图,在python终端里面输入 import _tkinter是可以的,但是打包以后会这样报错,

引入 tcl86t.dll和tk86t.dll 后问题解决

然后很容易会报错如下图,解决办法是一个一个的在自己的python路径中找到这些.py文件,然后执行:python -m compileall   +文件名.py      再把生成的.pyc文件拷到打包项目的相应位置

 

 setup.py  :

import sys
from cx_Freeze import setup, Executable
import os
os.environ['TCL_LIBRARY'] = "C:\\Users\\Administrator\\AppData\\Local\\Programs\\Python\\Python36\\tcl\\tcl8.6"
os.environ['TK_LIBRARY'] = "C:\\Users\\Administrator\\AppData\\Local\\Programs\\Python\\Python36\\tcl\\tk8.6"

include_files = [r"C:\Users\Administrator\AppData\Local\Programs\Python\Python36\DLLs\tcl86t.dll",
                 r"C:\Users\Administrator\AppData\Local\Programs\Python\Python36\DLLs\tk86t.dll"]

#打包所需要的第三库
build_exe_options = {"packages": ["glob", "os", "numpy", "datetime", "PIL", "matplotlib", "scipy", "_tkinter"],
                     "includes ": ['numpy','glob','os','datetime','datetime','PIL', "matplotlib", "matplotlib.pyplot" ,"scipy", "_tkinter"]}

base = None
if sys.platform == 'win32':
    base = 'Win32GUI'

options = {
    'build_exe': {
        'includes': 'atexit' ,'include_files': include_files
    }
}

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

setup(name='education',
      version='0.1',
      description='education software',
      options=options,
      executables=executables
      )

 

posted @ 2018-07-11 18:36  未名亚柳  阅读(3026)  评论(0编辑  收藏  举报