0%

使用pyinstaller打包python程序


常用的Python打包为exe工具有:

  1. py2exe
  2. pyinstaller
  3. cxfreeze

选择使用方便的pyinstaller作为打包工具。

安装

有两种方式:

  1. 官网下载后解压缩到需要的文件夹就可以执行,不需要安装。
  2. pip 方式安装升级: pip install pyinstallerpip install –upgrade pyinstaller

使用第二种方式安装pyinstaller,安装过程中遇到一些环境问题,主要是pip,google解决之。

python2.7 -m pip install -U setuptools
python3.4 -m pip install -U setuptools
python2.7 -m pip install pyinstaller
python3.4 -m pip install pyinstaller

执行pyinstaller可能遇到如下问题:

FileNotFoundError: Path or glob "/usr/include/python3.4m/pyconfig.h" not found or matches no files.

文章
解决:

sudo apt-get install libpython3.4-dev

使用

测试代码:

#!/usr/bin/env python3.4
# -*- coding: utf-8 -*-

from tkinter import *

root = Tk()
root.title('测试界面')
root.config(height=480,pady=10)

title = Label(root,text='测试控件',font='微软雅黑 -23 bold',fg='#fa8723')
title.pack()

root.mainloop()                         

执行打包命令:

✘ /data/OpenSourceCode/python/tkinter/test  > pyinstaller -F tk-1030.py
28 INFO: PyInstaller: 3.0
28 INFO: Python: 3.4.3
28 INFO: Platform: Linux-3.19.0-32-generic-x86_64-with-Ubuntu-15.04-vivid
28 INFO: wrote /data/OpenSourceCode/python/tkinter/test/tk-1030.spec
29 INFO: UPX is not available.
30 INFO: Extending PYTHONPATH with /data/OpenSourceCode/python/tkinter/test
31 INFO: checking Analysis
31 INFO: Building Analysis because out00-Analysis.toc is non existent
31 INFO: Initializing module dependency graph...
96 INFO: Initializing module graph hooks...
110 INFO: Analyzing base_library.zip ...
1864 INFO: Processing pre-find module path hook   distutils
3560 INFO: running Analysis out00-Analysis.toc
3647 INFO: Analyzing /data/OpenSourceCode/python/tkinter/test/tk-1030.py
3834 INFO: Looking for import hooks ...
3839 INFO: Processing hook   hook-sysconfig.py
3859 INFO: Processing hook   hook-pydoc.py
3860 INFO: Processing hook   hook-_tkinter.py
3934 INFO: checking Tree
3934 INFO: Building Tree because out00-Tree.toc is non existent
3934 INFO: Building Tree out00-Tree.toc
3954 INFO: checking Tree
3954 INFO: Building Tree because out01-Tree.toc is non existent
3954 INFO: Building Tree out01-Tree.toc
3963 INFO: Processing hook   hook-encodings.py
3983 INFO: Processing hook   hook-distutils.py
3984 INFO: Processing hook   hook-xml.py
4272 INFO: Processing hook   hook-xml.sax.py
4281 INFO: Looking for ctypes DLLs
4628 INFO: Analyzing run-time hooks ...
4631 INFO: Including run-time hook 'pyi_rth__tkinter.py'
4643 INFO: Looking for dynamic libraries
5008 INFO: Looking for eggs
5008 INFO: Python library not in binary depedencies. Doing additional searching...
5197 INFO: Using Python library /usr/lib/x86_64-linux-gnu/libpython3.4m.so.1.0
5204 INFO: Warnings written to /data/OpenSourceCode/python/tkinter/test/build/tk-1030/warntk-1030.txt
5218 INFO: checking PYZ
5218 INFO: Building PYZ because out00-PYZ.toc is non existent
5218 INFO: Building PYZ (ZlibArchive) /data/OpenSourceCode/python/tkinter/test/build/tk-1030/out00-PYZ.pyz
5430 INFO: checking PKG
5430 INFO: Building PKG because out00-PKG.toc is non existent
5430 INFO: Building PKG (CArchive) out00-PKG.pkg
10161 INFO: Bootloader /usr/local/lib/python3.4/dist-packages/PyInstaller/bootloader/Linux-64bit/run
10161 INFO: checking EXE
10161 INFO: Building EXE because out00-EXE.toc is non existent
10161 INFO: Building EXE from out00-EXE.toc
10167 INFO: Appending archive to EXE /data/OpenSourceCode/python/tkinter/test/dist/tk-1030

exe文件生成在dist文件夹中,执行命令验证正常:

✔ /data/OpenSourceCode/python/tkinter/test  > ./dist/tk-1030
  1. 关于python打包成exe的一点经验之谈
  2. PYINSTALLER 安装和使用
  3. py2exe Tutorial
  4. PyInstaller Manual
  5. PYInstaller Supported Packages
  6. cx_Freeze’s documentation