Skip to content

Commit 93e367b

Browse files
authored
Optimised Build Script to reduce exe size
1 parent fb31bbf commit 93e367b

File tree

1 file changed

+103
-33
lines changed

1 file changed

+103
-33
lines changed

pyinstaller-build-script.py

+103-33
Original file line numberDiff line numberDiff line change
@@ -1,33 +1,103 @@
1-
import os
2-
import subprocess
3-
4-
def run_pyinstaller_build():
5-
pyinstaller_command = [
6-
"pyinstaller",
7-
"--onefile",
8-
"--windowed",
9-
"--icon=icons/app_icon.ico",
10-
"main.py"
11-
]
12-
13-
try:
14-
os.system("rmdir /s /q dist")
15-
16-
subprocess.run(pyinstaller_command, check=True)
17-
print("Build completed successfully!")
18-
19-
os.rename("dist/main.exe", "dist/Writing Tools.exe")
20-
21-
os.system("mkdir dist\\icons")
22-
23-
os.system("copy /Y *.png dist")
24-
os.system("copy /Y icons\\*.* dist\\icons")
25-
26-
os.system("powershell Compress-Archive -Path dist -DestinationPath 'dist\\Writing Tools.zip'")
27-
28-
except subprocess.CalledProcessError as e:
29-
print(f"Build failed with error: {e}")
30-
sys.exit(1)
31-
32-
if __name__ == "__main__":
33-
run_pyinstaller_build()
1+
import os
2+
import subprocess
3+
import sys
4+
5+
6+
def run_pyinstaller_build():
7+
pyinstaller_command = [
8+
"pyinstaller",
9+
"--onefile",
10+
"--windowed",
11+
"--icon=icons/app_icon.ico",
12+
"--name=Writing Tools",
13+
"--clean",
14+
"--noconfirm",
15+
# Exclude unnecessary modules
16+
"--exclude-module", "tkinter",
17+
"--exclude-module", "unittest",
18+
"--exclude-module", "IPython",
19+
"--exclude-module", "jedi",
20+
"--exclude-module", "email_validator",
21+
"--exclude-module", "cryptography",
22+
"--exclude-module", "psutil",
23+
"--exclude-module", "pyzmq",
24+
"--exclude-module", "tornado",
25+
# Exclude modules related to PySide6 that are not used
26+
"--exclude-module", "PySide6.QtNetwork",
27+
"--exclude-module", "PySide6.QtXml",
28+
"--exclude-module", "PySide6.QtQml",
29+
"--exclude-module", "PySide6.QtQuick",
30+
"--exclude-module", "PySide6.QtQuickWidgets",
31+
"--exclude-module", "PySide6.QtPrintSupport",
32+
"--exclude-module", "PySide6.QtSql",
33+
"--exclude-module", "PySide6.QtTest",
34+
"--exclude-module", "PySide6.QtSvg",
35+
"--exclude-module", "PySide6.QtSvgWidgets",
36+
"--exclude-module", "PySide6.QtHelp",
37+
"--exclude-module", "PySide6.QtMultimedia",
38+
"--exclude-module", "PySide6.QtMultimediaWidgets",
39+
"--exclude-module", "PySide6.QtOpenGL",
40+
"--exclude-module", "PySide6.QtOpenGLWidgets",
41+
"--exclude-module", "PySide6.QtPositioning",
42+
"--exclude-module", "PySide6.QtLocation",
43+
"--exclude-module", "PySide6.QtSerialPort",
44+
"--exclude-module", "PySide6.QtWebChannel",
45+
"--exclude-module", "PySide6.QtWebSockets",
46+
"--exclude-module", "PySide6.QtWinExtras",
47+
"--exclude-module", "PySide6.QtNetworkAuth",
48+
"--exclude-module", "PySide6.QtRemoteObjects",
49+
"--exclude-module", "PySide6.QtTextToSpeech",
50+
"--exclude-module", "PySide6.QtWebEngineCore",
51+
"--exclude-module", "PySide6.QtWebEngineWidgets",
52+
"--exclude-module", "PySide6.QtWebEngine",
53+
"--exclude-module", "PySide6.QtBluetooth",
54+
"--exclude-module", "PySide6.QtNfc",
55+
"--exclude-module", "PySide6.QtWebView",
56+
"--exclude-module", "PySide6.QtCharts",
57+
"--exclude-module", "PySide6.QtDataVisualization",
58+
"--exclude-module", "PySide6.QtPdf",
59+
"--exclude-module", "PySide6.QtPdfWidgets",
60+
"--exclude-module", "PySide6.QtQuick3D",
61+
"--exclude-module", "PySide6.QtQuickControls2",
62+
"--exclude-module", "PySide6.QtQuickParticles",
63+
"--exclude-module", "PySide6.QtQuickTest",
64+
"--exclude-module", "PySide6.QtQuickWidgets",
65+
"--exclude-module", "PySide6.QtSensors",
66+
"--exclude-module", "PySide6.QtStateMachine",
67+
"--exclude-module", "PySide6.Qt3DCore",
68+
"--exclude-module", "PySide6.Qt3DRender",
69+
"--exclude-module", "PySide6.Qt3DInput",
70+
"--exclude-module", "PySide6.Qt3DLogic",
71+
"--exclude-module", "PySide6.Qt3DAnimation",
72+
"--exclude-module", "PySide6.Qt3DExtras",
73+
"main.py"
74+
]
75+
76+
try:
77+
# Remove previous build directories
78+
if os.path.exists('dist'):
79+
os.system("rmdir /s /q dist")
80+
if os.path.exists('build'):
81+
os.system("rmdir /s /q build")
82+
if os.path.exists('__pycache__'):
83+
os.system("rmdir /s /q __pycache__")
84+
85+
# Run PyInstaller
86+
subprocess.run(pyinstaller_command, check=True)
87+
print("Build completed successfully!")
88+
89+
# Clean up unnecessary files
90+
if os.path.exists('build'):
91+
os.system("rmdir /s /q build")
92+
if os.path.exists('__pycache__'):
93+
os.system("rmdir /s /q __pycache__")
94+
95+
# No need to copy data files manually since they are included
96+
# in the executable using --add-data
97+
98+
except subprocess.CalledProcessError as e:
99+
print(f"Build failed with error: {e}")
100+
sys.exit(1)
101+
102+
if __name__ == "__main__":
103+
run_pyinstaller_build()

0 commit comments

Comments
 (0)