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
addDataSym = ':' if platform.system() == 'Windows': addDataSym = ';' else: pass
name_cmd = ' --name TTEST' no_console = ' --noconsole'
bundleIdentify = " --osx-bundle-identifier com.lx.TTEST"
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_cmd + icon_cmd + name_cmd + no_console + dis_pathch + bundleIdentify
print('pyinstall CMD is:', pyinstallerRun)
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:
- 将该脚本文件至于需要打包文件的同级目录
- 修改程式里
icon
, AppName
, BundleId
等信息
通过执行上述脚本可以解决打包之后图标在Docker栏跳动的问题(动态加载,启动慢),但会导致安装包比较大。内部会将所需需要链接的库安装到打包文件中,不会去动态加载