python使用setuptools和nsis工具打包nuitka工程
Published in:2024-04-10 |
Words: 2k | Reading time: 10min | reading:

python使用setuptools和nsis工具打包nuitka工程

简介

setuptools 是官方提供的一个专业用于包分发的工具,若只从安装的角度来看,它的功能确实简单。它更大的意义是对包的分发很有用,定制化程序非常高,可用它进行版本包的发布。

Egg 格式是由 setuptools 在 2004 年引入,而 Wheel 格式是由 PEP427 在 2012 年定义。Wheel 的出现是为了替代 Egg,它的本质是一个zip包,其现在被认为是 Python 的二进制包的标准格式。

应用程序发布的时候,具备以下两种方式:

静态编译:把相关联的库一并引入可执行程序,虽然发布简单,编译出来只有一个exe文件。(特别注意:编译时,其依赖的库也需要使用静态包)。
动态编译:把相关联的库以dll的形式提供(linux下是.so)引入,不被包含进可执行程序,发布不方便,但可执行程序较小。

setuptools方式

1.使用以下代码

  • 在项目根目录运行
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    # -*- coding:utf-8 -*-
    from setuptools import setup,find_packages
    setup(
    name= "packageName", #你的python项目包名
    version = '1.0.0',
    description='No description',
    author="daimashiren",
    author_email='123456@gmail.com',
    url='xxx.com',
    packages = find_packages(),#find_packages()方法会自动寻找当前目录下名为packageName的包
    )

2. 运行以下命令:

1
python setup.py install 

测试

1
2
3
# cmd命令行输入以下命令尝试导入你的自定义包,看看能否成功被导入,如果,没有报说明No module named XXX 错误则说明python项目打包成功,否则则需要重新尝试以上步骤,重新打包直到能够导入为止。下图中自定义的"courseHelper_dev"包导入成功而自定义的"packageName"包则导入失败!
python
import packageName

检查easy_install.pth文件中有没有你的项目包名,pth后缀的文件记录的是python导入包时会检索的路径或包名,

NSIS软件

Nsis其实是使用一种脚本语言了,学习成本肯定是不低的。
采用NSIS + QT 制作安装程序实际上就是使用QT制作安装程序,而NSIS仅仅将制作的安装程序打包成一个exe。

下载安装

  • 下载地址
    1
    https://nsis.sourceforge.io/Download

使用 method 1

如图,启动nsis软件

选择打包zip,选择选项点击generate生成安装包

根据提示操作,结果如下:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
MakeNSIS v3.10 - Copyright 1999-2023 Contributors
See the file COPYING for license details.
Credits can be found in the Users Manual.

Processing config: C:\Program Files (x86)\NSIS\nsisconf.nsh
Processing script file: "C:\Users\ADMINI~1\AppData\Local\Temp\zne7EA0.tmp" (UTF16LE)

Processed 1 file, writing output (x86-unicode):

Output: "C:\Users\Administrator\PycharmProjects\spider_image_system\src\out\ui_main.dist.exe"
Install: 2 pages (128 bytes), 1 section (1 required) (2072 bytes), 181 instructions (5068 bytes), 210 strings (6906 bytes), 1 language table (262 bytes).

Using lzma compression.

EXE header size: 51200 / 38400 bytes
Install code: 2779 / 14900 bytes
Install data: 50727899 / 226716719 bytes
CRC (0xE230FDF8): 4 / 4 bytes

Total size: 50781882 / 226770023 bytes (22.3%)
(source ZIP size was 75374073 bytes)

使用 method 2

VNISEdit 可视化制作安装包向导使用

  • 下载地址
    1
    http://hmne.sourceforge.net
  • 按照提示操作,生成代码如下:
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42
    43
    44
    45
    46
    47
    48
    49
    50
    51
    52
    53
    54
    55
    56
    57
    58
    59
    60
    61
    62
    63
    64
    65
    66
    67
    68
    69
    70
    71
    72
    73
    74
    75
    76
    77
    78
    79
    80
    81
    82
    83
    84
    85
    86
    87
    88
    89
    90
    91
    92
    93
    94
    95
    96
    97
    98
    99
    100
    101
    102
    103
    104
    105
    106
    107
    108
    109
    110
    111
    112
    113
    114
    115
    116
    117
    118
    119
    120
    121
    122
    123
    124
    125
    126
    127
    128
    129
    130
    131
    132
    133
    134
    135
    136
    137
    138
    139
    140
    141
    142
    143
    144
    145
    146
    147
    148
    149
    150
    151
    152
    153
    154
    155
    156
    157
    158
    159
    160
    161
    162
    163
    164
    165
    166
    167
    168
    169
    170
    171
    172
    173
    174
    175
    176
    177
    178
    179
    180
    181
    182
    183
    184
    185
    ; Script generated by the HM NIS Edit Script Wizard.

    ; HM NIS Edit Wizard helper defines
    !define PRODUCT_NAME "spider image system"
    !define PRODUCT_VERSION "1.0"
    !define PRODUCT_PUBLISHER "zhaoqi.cao"
    !define PRODUCT_WEB_SITE "http://caozhaoqi.github.io"
    !define PRODUCT_DIR_REGKEY "Software\Microsoft\Windows\CurrentVersion\App Paths\ui_main.exe"
    !define PRODUCT_UNINST_KEY "Software\Microsoft\Windows\CurrentVersion\Uninstall\${PRODUCT_NAME}"
    !define PRODUCT_UNINST_ROOT_KEY "HKLM"
    !define PRODUCT_STARTMENU_REGVAL "NSIS:StartMenuDir"

    ; MUI 1.67 compatible ------
    !include "MUI.nsh"

    ; MUI Settings
    !define MUI_ABORTWARNING
    !define MUI_ICON "${NSISDIR}\Contrib\Graphics\Icons\modern-install.ico"
    !define MUI_UNICON "${NSISDIR}\Contrib\Graphics\Icons\modern-uninstall.ico"

    ; Welcome page
    !insertmacro MUI_PAGE_WELCOME
    ; License page
    !define MUI_LICENSEPAGE_CHECKBOX
    !insertmacro MUI_PAGE_LICENSE "C:\Users\Administrator\PycharmProjects\spider_image_system\src\out\ui_main.dist\SISSoftwareLicence.txt"
    ; Directory page
    !insertmacro MUI_PAGE_DIRECTORY
    ; Start menu page
    var ICONS_GROUP
    !define MUI_STARTMENUPAGE_NODISABLE
    !define MUI_STARTMENUPAGE_DEFAULTFOLDER "spider image system"
    !define MUI_STARTMENUPAGE_REGISTRY_ROOT "${PRODUCT_UNINST_ROOT_KEY}"
    !define MUI_STARTMENUPAGE_REGISTRY_KEY "${PRODUCT_UNINST_KEY}"
    !define MUI_STARTMENUPAGE_REGISTRY_VALUENAME "${PRODUCT_STARTMENU_REGVAL}"
    !insertmacro MUI_PAGE_STARTMENU Application $ICONS_GROUP
    ; Instfiles page
    !insertmacro MUI_PAGE_INSTFILES
    ; Finish page
    !define MUI_FINISHPAGE_RUN "$INSTDIR\ui_main.exe"
    !insertmacro MUI_PAGE_FINISH

    ; Uninstaller pages
    !insertmacro MUI_UNPAGE_INSTFILES

    ; Language files
    !insertmacro MUI_LANGUAGE "English"

    ; MUI end ------

    Name "${PRODUCT_NAME} ${PRODUCT_VERSION}"
    OutFile "Setup.exe"
    InstallDir "$PROGRAMFILES\spider image system"
    InstallDirRegKey HKLM "${PRODUCT_DIR_REGKEY}" ""
    ShowInstDetails show
    ShowUnInstDetails show

    Section "MainSection" SEC01
    SetOutPath "$INSTDIR"
    SetOverwrite ifnewer
    File "..\PycharmProjects\spider_image_system\src\out\ui_main.dist\vcruntime140_1.dll"
    File "..\PycharmProjects\spider_image_system\src\out\ui_main.dist\vcruntime140.dll"
    File "..\PycharmProjects\spider_image_system\src\out\ui_main.dist\unicodedata.pyd"
    File "..\PycharmProjects\spider_image_system\src\out\ui_main.dist\ui_main.exe"
    File "..\PycharmProjects\spider_image_system\src\out\ui_main.dist\select.pyd"
    File "..\PycharmProjects\spider_image_system\src\out\ui_main.dist\qt5widgets.dll"
    File "..\PycharmProjects\spider_image_system\src\out\ui_main.dist\qt5svg.dll"
    File "..\PycharmProjects\spider_image_system\src\out\ui_main.dist\qt5gui.dll"
    File "..\PycharmProjects\spider_image_system\src\out\ui_main.dist\qt5core.dll"
    File "..\PycharmProjects\spider_image_system\src\out\ui_main.dist\qt5charts.dll"
    File "..\PycharmProjects\spider_image_system\src\out\ui_main.dist\python39.dll"
    File "..\PycharmProjects\spider_image_system\src\out\ui_main.dist\python3.dll"
    File "..\PycharmProjects\spider_image_system\src\out\ui_main.dist\pyexpat.pyd"
    File "..\PycharmProjects\spider_image_system\src\out\ui_main.dist\msvcp140_1.dll"
    File "..\PycharmProjects\spider_image_system\src\out\ui_main.dist\msvcp140.dll"
    File "..\PycharmProjects\spider_image_system\src\out\ui_main.dist\libssl-1_1.dll"
    File "..\PycharmProjects\spider_image_system\src\out\ui_main.dist\libffi-7.dll"
    File "..\PycharmProjects\spider_image_system\src\out\ui_main.dist\libcrypto-1_1.dll"
    File "..\PycharmProjects\spider_image_system\src\out\ui_main.dist\_uuid.pyd"
    File "..\PycharmProjects\spider_image_system\src\out\ui_main.dist\_ssl.pyd"
    File "..\PycharmProjects\spider_image_system\src\out\ui_main.dist\_socket.pyd"
    File "..\PycharmProjects\spider_image_system\src\out\ui_main.dist\_queue.pyd"
    File "..\PycharmProjects\spider_image_system\src\out\ui_main.dist\_overlapped.pyd"
    File "..\PycharmProjects\spider_image_system\src\out\ui_main.dist\_lzma.pyd"
    File "..\PycharmProjects\spider_image_system\src\out\ui_main.dist\_hashlib.pyd"
    File "..\PycharmProjects\spider_image_system\src\out\ui_main.dist\_elementtree.pyd"
    File "..\PycharmProjects\spider_image_system\src\out\ui_main.dist\_decimal.pyd"
    File "..\PycharmProjects\spider_image_system\src\out\ui_main.dist\_ctypes.pyd"
    File "..\PycharmProjects\spider_image_system\src\out\ui_main.dist\_cffi_backend.pyd"
    File "..\PycharmProjects\spider_image_system\src\out\ui_main.dist\_bz2.pyd"
    File "..\PycharmProjects\spider_image_system\src\out\ui_main.dist\_asyncio.pyd"

    ; Shortcuts
    !insertmacro MUI_STARTMENU_WRITE_BEGIN Application
    CreateDirectory "$SMPROGRAMS\$ICONS_GROUP"
    CreateShortCut "$SMPROGRAMS\$ICONS_GROUP\spider image system.lnk" "$INSTDIR\ui_main.exe"
    CreateShortCut "$DESKTOP\spider image system.lnk" "$INSTDIR\ui_main.exe"
    !insertmacro MUI_STARTMENU_WRITE_END
    SectionEnd

    Section "RegComOcx" SEC02

    SetOutPath "$INSTDIR"

    RegDLL "$INSTDIR\laoheitanActiveX.ocx"

    SectionEnd

    Section -AdditionalIcons
    !insertmacro MUI_STARTMENU_WRITE_BEGIN Application
    WriteIniStr "$INSTDIR\${PRODUCT_NAME}.url" "InternetShortcut" "URL" "${PRODUCT_WEB_SITE}"
    CreateShortCut "$SMPROGRAMS\$ICONS_GROUP\Website.lnk" "$INSTDIR\${PRODUCT_NAME}.url"
    CreateShortCut "$SMPROGRAMS\$ICONS_GROUP\Uninstall.lnk" "$INSTDIR\uninst.exe"
    !insertmacro MUI_STARTMENU_WRITE_END
    SectionEnd

    Section -Post
    WriteUninstaller "$INSTDIR\uninst.exe"
    WriteRegStr HKLM "${PRODUCT_DIR_REGKEY}" "" "$INSTDIR\ui_main.exe"
    WriteRegStr ${PRODUCT_UNINST_ROOT_KEY} "${PRODUCT_UNINST_KEY}" "DisplayName" "$(^Name)"
    WriteRegStr ${PRODUCT_UNINST_ROOT_KEY} "${PRODUCT_UNINST_KEY}" "UninstallString" "$INSTDIR\uninst.exe"
    WriteRegStr ${PRODUCT_UNINST_ROOT_KEY} "${PRODUCT_UNINST_KEY}" "DisplayIcon" "$INSTDIR\ui_main.exe"
    WriteRegStr ${PRODUCT_UNINST_ROOT_KEY} "${PRODUCT_UNINST_KEY}" "DisplayVersion" "${PRODUCT_VERSION}"
    WriteRegStr ${PRODUCT_UNINST_ROOT_KEY} "${PRODUCT_UNINST_KEY}" "URLInfoAbout" "${PRODUCT_WEB_SITE}"
    WriteRegStr ${PRODUCT_UNINST_ROOT_KEY} "${PRODUCT_UNINST_KEY}" "Publisher" "${PRODUCT_PUBLISHER}"
    SectionEnd


    Function un.onUninstSuccess
    HideWindow
    MessageBox MB_ICONINFORMATION|MB_OK "$(^Name) 已成功地从你的计算机移除。"
    FunctionEnd

    Function un.onInit
    MessageBox MB_ICONQUESTION|MB_YESNO|MB_DEFBUTTON2 "你确实要完全移除 $(^Name) ,其及所有的组件?" IDYES +2
    Abort
    FunctionEnd

    Section Uninstall
    !insertmacro MUI_STARTMENU_GETFOLDER "Application" $ICONS_GROUP
    Delete "$INSTDIR\${PRODUCT_NAME}.url"
    Delete "$INSTDIR\uninst.exe"
    Delete "$INSTDIR\_asyncio.pyd"
    Delete "$INSTDIR\_bz2.pyd"
    Delete "$INSTDIR\_cffi_backend.pyd"
    Delete "$INSTDIR\_ctypes.pyd"
    Delete "$INSTDIR\_decimal.pyd"
    Delete "$INSTDIR\_elementtree.pyd"
    Delete "$INSTDIR\_hashlib.pyd"
    Delete "$INSTDIR\_lzma.pyd"
    Delete "$INSTDIR\_overlapped.pyd"
    Delete "$INSTDIR\_queue.pyd"
    Delete "$INSTDIR\_socket.pyd"
    Delete "$INSTDIR\_ssl.pyd"
    Delete "$INSTDIR\_uuid.pyd"
    Delete "$INSTDIR\libcrypto-1_1.dll"
    Delete "$INSTDIR\libffi-7.dll"
    Delete "$INSTDIR\libssl-1_1.dll"
    Delete "$INSTDIR\msvcp140.dll"
    Delete "$INSTDIR\msvcp140_1.dll"
    Delete "$INSTDIR\pyexpat.pyd"
    Delete "$INSTDIR\python3.dll"
    Delete "$INSTDIR\python39.dll"
    Delete "$INSTDIR\qt5charts.dll"
    Delete "$INSTDIR\qt5core.dll"
    Delete "$INSTDIR\qt5gui.dll"
    Delete "$INSTDIR\qt5svg.dll"
    Delete "$INSTDIR\qt5widgets.dll"
    Delete "$INSTDIR\select.pyd"
    Delete "$INSTDIR\ui_main.exe"
    Delete "$INSTDIR\unicodedata.pyd"
    Delete "$INSTDIR\vcruntime140.dll"
    Delete "$INSTDIR\vcruntime140_1.dll"

    Delete "$SMPROGRAMS\$ICONS_GROUP\Uninstall.lnk"
    Delete "$SMPROGRAMS\$ICONS_GROUP\Website.lnk"
    Delete "$DESKTOP\spider image system.lnk"
    Delete "$SMPROGRAMS\$ICONS_GROUP\spider image system.lnk"

    RMDir "$SMPROGRAMS\$ICONS_GROUP"
    RMDir "$INSTDIR"

    DeleteRegKey ${PRODUCT_UNINST_ROOT_KEY} "${PRODUCT_UNINST_KEY}"
    DeleteRegKey HKLM "${PRODUCT_DIR_REGKEY}"
    SetAutoClose true
    SectionEnd

编译并运行生成exe

  • 点击如下
  • 生成完成后,在脚本同目录点击setup.exe即可安装

See

Prev:
python中使用自定义线程
Next:
使用pyinstaller打包python程序并发布