使用PyInstaller 將python程序 .py轉爲 .exe

本文來自 Alliot's blog
最近常常用到一個.py程序,可是每次在不一樣電腦上用,但願能把Python腳本發佈爲脫離Python平臺運行的可執行程序,好比單個exe。PyInstalle知足要求。
PyInstaller自己並不屬於Python包。在安裝 pyinstaller以前需把python環境配置好。html

安裝pyinstaller

下載pyinstaller

解壓到F:PyInstaller-2.1(自選)(能夠去官網下載最新版)python

安裝pywin32

pywin32-217.win32-py2.7.exe:點擊下載post

安裝pyinstaller

  一、進入cmd測試

cd F:\PyInstaller-2.1
python pyinstaller.py --console --onefile  test.py

若是提示:ui

Usage: python pyinstaller.py [opts] <scriptname> [ <scriptname> ...] | <specfile>
pyinstaller.py: error: Requires at least one scriptname file or exactly one .spec-file.net

則說明安裝完成了。code

測試打包

  一、文件放在當前目錄的pyinstaller-2.1文件夾裏面htm

cd F:\PyInstaller-2.1
python pyinstaller.py --console --onefile  test.py

  二、命令運行成功後會生成一個test文件夾。在這個文件夾下面會有一個名爲dist的文件夾,此文件夾下面有轉換好的test.exe
  三、上面編譯出來的exe可以正常運行了,但帶一個黑色的console,如下從新編譯,加入--windowed --icon,取消--consoleblog

python pyinstaller.py  -w  --onefile --icon="my.ico" test.py

其中my.ico是你要給他加的自定義圖標文件。ip

相關文章
相關標籤/搜索