python打包成exe


目前有三種方法可以實現python打包成exe,分別為

  • py2exe
  • Pyinstaller
  • cx_Freeze

其中沒有一個是完美的

1.py2exe的話不支持egg類型的python庫

2.Pyinstaller2.1打包成單獨的exe后不支持中文路徑,最新版的在win10下打包的exe不可以在之前版本的windows上運行。不過還好的是2.0版本支持中文路徑,不過2.0版本不支持安裝,需要單獨使用

3.cx_Freeze無法打包成單獨的exe,只能打包成msi安裝文件

1.py2exe

關於py2exe,首先要注意的是安裝的版本,因為即使你用的是python2的pip來安裝py2exe,它還是會給你裝上py2exe 0.9xx ,而這個版本是python3使用的,python2 會報錯。python2的兼容版本是0.6的

操作方法:

1.首先就是把你寫的一堆py文件放到一個文件夾中,如果你寫的是窗口程序,也不需要把后綴改成pyw

2.然后在目錄下建一個setup.py文件:

gui程序如下

from distutils.core import setup
import py2exe
import sys
 
#this allows to run it with a simple double click.
sys.argv.append('py2exe')
 
py2exe_options = {
        "includes": ["sip"],
        "dll_excludes": ["MSVCP90.dll",],
        "compressed": 1,
        "optimize": 2,
        "ascii": 0
        }
 
setup(
      name = 'E_hentai Downloader',
      version = '1.0',
      windows = ['E_hentai.py',], 
      zipfile = None,
      options = {'py2exe': py2exe_options}
      )

控制台如下

from distutils.core import setup
import py2exe
import sys

# this allows to run it with a simple double click.
sys.argv.append('py2exe')

py2exe_options = {
    "includes": ["sip"],
    "dll_excludes": ["MSVCP90.dll", ],
    "compressed": 1,
    "optimize": 2,
    "ascii": 0,
    "bundle_files": 1
}

setup(
    name='Anhona Downloader',
    version='1.0',
    console=["AnhonaDownloader.py"],
    zipfile=None,
    options={'py2exe': py2exe_options}
)

上面的option選項中還可以加上一句
"bundle_files": 1
這個代表打包成一個單文件,需要注意的是,在打包pyqt的時候,最好不要加上這個選項,因為這個相當於靜態編譯,不
會去連接外面的庫,所以會導致編譯出來后無法加載jpg圖片,即使你添加了addLibraryPath。

3.在命令行里面運行 python setup.py py2exe 即可打包完工(最好進入當前文件夾,這樣可以在當前目錄生成exe)

附加:
如果你是打包的pyqt,記得把plugins拷貝到應用程序目錄下,包括pyqt4目錄下的qt.conf(等同於addLibraryPath)

2.pyinstaller

這個比較容易使用,如果是控制台程序支持輸入下面的命令

pyinstaller --console --onefile --icon="my.ico" xxxxx.py

3.cx_freeze

這個和py2exe比較像,需要一個setup.py文件,內容如下

# -*- coding: utf-8 -*-

import sys
from cx_Freeze import setup, Executable

# Dependencies are automatically detected, but it might need fine tuning.
build_exe_options = {"packages": ["os"], "excludes": ["tkinter"]}

# GUI applications require a different base on Windows (the default is for a
# console application).
setup(name="AnhonaDownloader",
      version="1.0",
      description="My GUI application!",
      options={"build_exe": build_exe_options},
      executables=[Executable("AnhonaDownloader.py")])

然后輸入

setup.py build

如果需要作出安裝包需要加上bdist_msi


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM