在CX_Freeze中从Python 3.6脚本创建单个exe

时间:2017-04-07 07:21:40

标签: python python-3.x cx-freeze

我有一个Python脚本,我想编译成一个可执行文件,我已经读过PyInstaller是最好的选择,但不幸的是CX_Freeze是我发现使用Python 3.6的唯一编译器。

有没有办法用CX_Freeze做到这一点?

2 个答案:

答案 0 :(得分:4)

首先你必须拥有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

答案 1 :(得分:0)

cx_freeze将在目标文件夹中生成一个可执行文件和所有相关文件。要将其打包为“单个”可执行文件,我使用了免费的Cameyo Packaging实用程序来虚拟化应用程序。为了获得更专业的效果,我还使用了Nullsoft Scriptable Install System (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