簡體   English   中英

從CX_Freeze中的Python 3.6腳本創建單個exe

[英]Create single exe from Python 3.6 scripts in CX_Freeze

我有一個Python腳本,我想將其編譯成一個可執行文件,我已經讀到PyInstaller是對此的最佳選擇,但不幸的是CX_Freeze是我發現可與Python 3.6一起使用的唯一編譯器。

有沒有辦法用CX_Freeze做到這一點?

首先,您必須具有cx_freeze 5.0.1,因為它支持python 3.6。

然后,就像任何3.X版本。 將此代碼放在setup.py文件中並替換:

"prog.py" 

與您的主要腳本名稱。

保重,不要遺忘,如果要去控制台,它應該是

if sys.platform == "win32":
    base = "console" 

這是代碼:

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).
base = None
if sys.platform == "win32":
    base = "Win32GUI"


setup(  name = "my prog",
    version = "1.0",
    description = "My application!",
    options = {"build_exe": build_exe_options},
    executables = [Executable("prog.py", base = base)])

打開命令提示符寫入:

cd your directory path 
python setup.py build

cx_freeze將在目標文件夾中生成一個可執行文件和所有相關文件。 要將其打包為“單個”可執行文件,我使用了免費的Cameyo Packaging實用程序對應用程序進行虛擬化。 為了獲得更專業的效果,我還使用Nullsoft可腳本安裝系統(NSIS)系統。

下面的示例代碼采用了我開發的名為Timer的Python 3.7應用程序的build目錄的內容。 只需將源目錄和程序名稱替換為等效名稱即可。

;------------------------------------------------------------------------------
; Purpose:      Script to package a Python cx_freeze application into a single
;               executable covering initial install, upgrades, and execution
; Author:       Ed Sheehan
; Revision:     2018-12-14 Initial development
;------------------------------------------------------------------------------

!include "MUI2.nsh" ;Required for splash screen

CRCCheck on ;Prevent execution if package corrupted or manipulated by third party
RequestExecutionLevel user ;Invoke as user to prevent request for elevated rights
SilentInstall silent ;Suppress standard progress windows of installer 

Name "Meeting Master"
OutFile Timer.exe

;Set the package icon and property details
!define MUI_ABORTWARNING
!define MUI_ICON "D:\Build\favicon.ico"
!insertmacro MUI_PAGE_INSTFILES
!insertmacro MUI_LANGUAGE "English"
VIProductVersion "2018.12.14.01"
VIAddVersionKey /LANG=${LANG_ENGLISH} "ProductName" "Timer"
VIAddVersionKey /LANG=${LANG_ENGLISH} "FileVersion" "2018.12.14.01"
VIAddVersionKey /LANG=${LANG_ENGLISH} "FileDescription" "Meeting agenda timer"
VIAddVersionKey /LANG=${LANG_ENGLISH} "ProductVersion" "0.001.a"
VIAddVersionKey /LANG=${LANG_ENGLISH} "LegalCopyright" "Copyright (c)2018 Navem Pty Ltd"

;Write application to users roaming location and save splash screen in temporary area
Function .onInit
    SetOutPath $APPDATA\Timer
    InitPluginsDir
    File "/oname=$PluginsDir\splash.jpg" "D:\Build\splash.jpg"
FunctionEnd

;Extract / upgrade / run the application
Section "Installer Section"
    newadvsplash::show /NOUNLOAD 5000 500 500 -2 /BANNER "$PLUGINSDIR\splash.jpg" ;Long running splash screen if first time install or replacement (upgrade)
    IfFileExists $APPDATA\Timer\Ver_2018_12_14_001 Launcher 0 ;Run immediately if installed version file matches
    IfFileExists $APPDATA\Timer 0 NewInst ;Remove if different version installed and then install this version and run

    ;Remove old version (remember RMDIR will not work on active OurPath)
    SetOutPath $APPDATA
    RMDir /r $APPDATA\Timer
    SetOutPath $APPDATA\Timer

NewInst:
    ;Install this version
    File /r D:\Build\*.*
    Goto Skip

Launcher:
    ;Short pause to ensure splash screen is visible
    Sleep 500

Skip:
    ;Launch the application and ensure splash screen is cleared
    Exec $APPDATA\Timer\Timer.exe
    Sleep 1000
    newadvsplash::stop /FADEOUT

SectionEnd

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM