如何打包python应用(二)

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/zhulove86/article/details/53885246

对于python编程之后,除了在Python IDE中运行之外,可以将python的程序打包为python应用,直接在PC环境上运行。对于python打包目前有很多第三方库文件直接使用。在这里主要介绍一下cx freeze和pyinstaller两种

关于cxfreeze使用如何打包python应用(一)

pyinstaller:

1. 安装:
方法一:直接使用pip
pip install pyinstaller

方法二:下载源码安装
在github中打包下载,解压到电脑中,运行命令行进入此目录。
//进入子目录 bootloader
cd bootloader
//build the bootloader 运行
python ./waf configure build install
//重新进入根目录
cd ..
//安装pyinstaller
python setup.py install

2. pyinstaller打包:
python源文件,路径C:\Python33\Lib\site-packages\cx_Freeze\samples\eduactionData

包含六个源文件(自己编写的源程序代码)mainPage.py,creatDetailTable.py,creatTable.py,databaseOpetation.py,dataManager.py,dataManager.py

方法一:直接使用pyinstaller命令直接打包:
pyinstaller -F -w mainPage.py creatDetailTable.py creatTable.py databaseOpetation.py dataManager.py dataManager.py

方法二:使用脚本命令打包:
pyinstaller makeApplication.spec
其中makeApplication.spec脚本内容如下:

# -*- mode: python -*-

block_cipher = None


a = Analysis(['creatDetailTable.py', 'creatTable.py', 'databaseOpetation.py', 'dataManager.py',  'fileReader.py', 'fileSelectedWindow.py', 'mainPage.py', ],
             pathex=['C:\\Python33\\Lib\\site-packages\\cx_Freeze\\samples\\education'],
             binaries=None,
             datas=None,
             hiddenimports=[],
             hookspath=[],
             runtime_hooks=[],
             excludes=[],
             win_no_prefer_redirects=False,
             win_private_assemblies=False,
             cipher=block_cipher)
pyz = PYZ(a.pure, a.zipped_data,
             cipher=block_cipher)
exe = EXE(pyz,
          a.scripts,
          a.binaries,
          a.zipfiles,
          a.datas,
          name='education',
          debug=False,
          strip=False,
          upx=True,
          console=False )

3. 打包脚本说明:
方法一说明:
-F :将可执行文件和所需dll打包成一个可执行文件,否则exe文件会和其他dll文件在同一路径中。
-w :打包为window平台文件
方法二说明:
Analysis: 需要打包的源文件
pathex: 源文件路径
EXE :可执行文件相关信息

4. 打包过程:
打包结果:

文件目录:
│  creatDetailTable.py
│  creatTable.py
│  databaseOpetation.py
│  dataManager.py
│  fileReader.py
│  fileSelectedWindow.py
│  mainPage.py
│  makeApplication.spec
│  
├─build
│  └─makeApplication
│          base_library.zip
│          makeApplication.exe.manifest
│          out00-Analysis.toc
│          out00-EXE.toc
│          out00-PKG.pkg
│          out00-PKG.toc
│          out00-PYZ.pyz
│          out00-PYZ.toc
│          warnmakeApplication.txt
│          
├─dist
│      education.exe
│      
└─__pycache__
        creatDetailTable.cpython-33.pyc
        creatTable.cpython-33.pyc
        databaseOpetation.cpython-33.pyc
        dataManager.cpython-33.pyc
        fileReader.cpython-33.pyc
        fileSelectedWindow.cpython-33.pyc
        mainPage.cpython-33.pyc

dist/ 目录中education.exe为打包的可执行文件
pycache/ 目录为打包过程生成文件
build/ 目录也为打包过程生成文件

5. 程序运行:
在win7系统上打包生成education.exe,在xp系统上也能正常运行

参考资源:

https://pyinstaller.readthedocs.io/en/stable/usage.html 官方网站

猜你喜欢

转载自blog.csdn.net/zhulove86/article/details/53885246