將自己寫的python文件壓縮成exe有兩種方法:
1、使用pyinstaller
step1:安裝pyinstaller,在cmd窗口使用pip install pyinstaller安裝
step2:cd 到你的文件目錄cd D:\py\python testcases\Slice
step3:運行pyinstaller -F SliceFile.py,注意-F是大寫
step4:看結果,結果在新生成的文件夾dist下就有SliceFile.exe
補充一下后來發現的問題,運行pyinstaller -F SliceFile.py后的exe打開時總是先有一個cmd窗口出現,要去掉的話應該用運行pyinstaller -F -w SliceFile.py
2、使用py2exe
step1:安裝py2exe,在cmd窗口使用pip install py2exe安裝
step2:在你寫的py文件所在目錄下新建一個setup.py文件,添加內容為
from distutils.core import setup
import py2exe
setup(console=["SliceFile.py"])
step3:cd 到你的文件目錄cd D:\py\python testcases\Slice
step4:運行setup.py py2exe
step5:看結果,結果在新生成的文件夾dist下就有SliceFile.exe
兩種方法都很簡單,但是使用py2exe時我沒有成功,原因是py2exe只有python 3.4版本以前的,我的是Python 3.5.2版本,所以使用的是pyinstaller,測試通過