Python-pyinstller打包窗口应用

Python-pyinstller打包窗口应用,本文解决的是在MacOS上第一次启动APP时,在Docker栏上跳动几下再次启动的问题。

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
import os
import subprocess
import platform
import time

current_dir = os.path.dirname(os.path.abspath(__file__))
current_path = os.path.abspath(__file__)
print('current_dir is:', current_dir)
pyinstallerCMD = 'pyinstaller'

# 配置桌面图标
icon_path = os.path.join(current_dir, 'u.icns')
icon_cmd = ' --icon ' + icon_path


# 配置脚本文件
main_Path = os.path.join(current_dir, 'main.py')
main_cmd = ' ' + main_Path


# plist_path = os.path.join(current_dir, "resource", 'Info.plist')
# version_path = os.path.join(current_dir, "resource", 'version.txt')
addDataSym = ':'
if platform.system() == 'Windows':
addDataSym = ';'
else:
pass

# `TTEST` 为打包之后 APP 的名称
name_cmd = ' --name TTEST'
no_console = ' --noconsole'
# plist_cmd = " --info-plist " + plist_path

# MacOS 上需要配置bundleID 信息
bundleIdentify = " --osx-bundle-identifier com.lx.TTEST"
# version_cmd = " --version-file " + version_path


# 确保每次打包生成的Product在对应时间的目录之下
currentTime = time.strftime('%Y-%m-%d-%H%M%S', time.localtime(time.time()))
distDir = os.path.join(current_dir, "product",currentTime)
if not os.path.exists(distDir):
try:
os.makedirs(distDir)

except OSError as e:
print('OSError is:', e)
else:
print('{} created success'.format(distDir))
else:
print('dist dir is exists')

dis_pathch = ' --distpath=' + distDir
# pyinstallerRun = pyinstallerCMD + main_Path + name_cmd + no_console + bundleIdentify + dis_pathch
pyinstallerRun = pyinstallerCMD + main_cmd + icon_cmd + name_cmd + no_console + dis_pathch + bundleIdentify

print('pyinstall CMD is:', pyinstallerRun)


# 通过Shell 去执行具体打包应用
try:
p = subprocess.Popen(pyinstallerRun, shell=True, stdout=subprocess.PIPE, stderr=subprocess.STDOUT, universal_newlines=True)

while True:
output = p.stdout.readline()
if output == '' and p.poll() is not None:
break
if output:
print(output.strip())

except Exception as e:
print('pyinstaller Exception:', e)
else:
print('pyinstaller success!!!')

NOTE:

  1. 将该脚本文件至于需要打包文件的同级目录
  2. 修改程式里 icon, AppName, BundleId 等信息
    通过执行上述脚本可以解决打包之后图标在Docker栏跳动的问题(动态加载,启动慢),但会导致安装包比较大。内部会将所需需要链接的库安装到打包文件中,不会去动态加载

Python-pyinstller打包窗口应用
https://jackiedai.github.io/2025/03/21/011Python/010Python-pyinstller打包窗口应用/
Author
lingXiao
Posted on
March 21, 2025
Licensed under