cx_freeze打包
安装
1、安装cx_freeze
pip install cx_freeze
2、生成cxfreeze.bat。在python安装文件夹下的Scripts路径下运行
python cxfreeze-postinstall
3、生成的cxfreeze.bat文件内容如下:
@echo off
C:\Anaconda3\python.exe C:\Anaconda3\Scripts\cxfreeze %*
4、通过cxfreeze --version命令查询cxfreeze版本,若能正确查询cx_freeze版本,即可以判断安装成功。
C:\Users\Administrator> cxfreeze --version
cxfreeze 5.1.1
Copyright (c) 2007-2017 Anthony Tuininga. All rights reserved.
Copyright (c) 2001-2006 Computronix Corporation. All rights reserved.
打包
1、打包命令
F:\Python35\python3.exe F:\Python35\Scripts\cxfreeze AviVideo.py --target-dir="./AviVideo" --target-name="AVI.exe" --include-modules="numpy" --icon=1.ico
# F:\Python35\python3.exe :python安装路径和运行程序。可缺省
# F:\Python35\Scripts\cxfreeze:cx_freeze程序路径和运行程序
# AviVideo.py:需要被打包的程序
# --target-dir="./AviVideo":被打包的程序存放路径
# --target-name="AVI.exe":生成的exe文件名称,该参数缺省时同被打包文件名一致。可缺省
# --include-modules="numpy":打包模块列表,可用逗号分开。可缺省
# --icon=1.ico:打包的exe文件添加图标。可缺省
2、 简易打包命令:
cxfreeze AviVideo.py --target-dir="./AviVideo"
# cxfreeze:打包命令
# AviVideo.py:需要打包的文件
# --target-dir="./AviVideo":生成exe文件和辅助模块文件存放位置
cx_freeze打包文件遇到的问题
1、正确生成exe文件后,双击闪退。查明闪退原因
# 查询闪退原因,随后根据实际情况解决相应问题。
# 创建test.py文件
# 修改需要执行的exe文件路径
# 使用python运行该程序即可打印出闪退原因
import os
result=os.popen(r"G:\study\python\111\AviVideo\AviVideo.exe")
print(result.read())
2、运行生成的exe文件闪退,报错“ImportError: cannot import name '_methods'”
Traceback (most recent call last):
File "C:\Anaconda3\lib\site-packages\cx_Freeze\initscripts\__startup__.py", line 14, in run
module.run()
File "C:\Anaconda3\lib\site-packages\cx_Freeze\initscripts\Console.py", line 26, in run
exec(code, m.__dict__)
File "AviVideo.py", line 3, in <module>
import numpy
File "numpy\__init__.py", line 142, in <module>
from . import add_newdocs
File "numpy\add_newdocs.py", line 13, in <module>
from numpy.lib import add_newdoc
File "numpy\lib\__init__.py", line 8, in <module>
from .type_check import *
File "numpy\lib\type_check.py", line 11, in <module>
import numpy.core.numeric as _nx
File "numpy\core\__init__.py", line 38, in <module>
from . import numeric
File "numpy\core\numeric.py", line 2931, in <module>
from . import fromnumeric
File "numpy\core\fromnumeric.py", line 15, in <module>
from . import _methods
ImportError: cannot import name '_methods'
原因:打包的文件里辅助模块numpy中的lib\numpy\core下_methods.pyc文件没有打包成功
解决办法:1、 找到python中numpy模块安装路径下的methods.cpython-35.pyc文件,例如 C:Python35\Lib\site-packages\numpy\core_pycache_methods.cpython-35.pyc
2、将_methods.cpython-35.pyc文件复制到生成exe文件的lib\numpy\core文件夹下,并重命名为_methods.pyc
2、运行生成的exe文件闪退,报错“ImportError: cannot import name 'format'”
Traceback (most recent call last):
File "C:\Anaconda3\lib\site-packages\cx_Freeze\initscripts\__startup__.py", line 14, in run
module.run()
File "C:\Anaconda3\lib\site-packages\cx_Freeze\initscripts\Console.py", line 26, in run
exec(code, m.__dict__)
File "AviVideo.py", line 3, in <module>
import numpy
File "numpy\__init__.py", line 142, in <module>
from . import add_newdocs
File "numpy\add_newdocs.py", line 13, in <module>
from numpy.lib import add_newdoc
File "numpy\lib\__init__.py", line 24, in <module>
from .npyio import *
File "numpy\lib\npyio.py", line 13, in <module>
from . import format
ImportError: cannot import name 'format'
原因:打包的文件里辅助模块numpy中的lib\numpy\lib下format.pyc文件没有打包成功
解决办法:1、 找到python中numpy模块安装路径下的format.cpython-35.pyc文件,例如 C:Python35\Lib\site-packages\numpy\lib_pycache\format.cpython-35.pyc
2、将format.cpython-35.pyc文件复制到生成exe文件的lib\numpy\lib文件夹下,并重命名为format.pyc
pyinstaller打包
安装
1、安装pyinstaller
pip install pyinstaller
2、通过pyinstaller -h 查询模块使用方法
C:\Users\Administrator>pyinstaller
usage: pyinstaller [-h] [-v] [-D] [-F] [--specpath DIR] [-n NAME]
[--add-data <SRC;DEST or SRC:DEST>]
[--add-binary <SRC;DEST or SRC:DEST>] [-p DIR]
[--hidden-import MODULENAME]
[--additional-hooks-dir HOOKSPATH]
[--runtime-hook RUNTIME_HOOKS] [--exclude-module EXCLUDES]
[--key KEY] [-d] [-s] [--noupx] [-c] [-w]
[-i <FILE.ico or FILE.exe,ID or FILE.icns>]
[--version-file FILE] [-m <FILE or XML>] [-r RESOURCE]
[--uac-admin] [--uac-uiaccess] [--win-private-assemblies]
[--win-no-prefer-redirects]
[--osx-bundle-identifier BUNDLE_IDENTIFIER]
[--runtime-tmpdir PATH] [--distpath DIR]
[--workpath WORKPATH] [-y] [--upx-dir UPX_DIR] [-a]
[--clean] [--log-level LEVEL]
scriptname [scriptname ...]
......
打包
1、打包命令,生成的可执行文件在dist文件夹下。
# 所有辅助模块文件统一打包成一个exe文件。
pyinstaller -F xxxx.py
# 简易打包命令
pyinstaller xxxx.py