Create flask app installer using pyinstaller
Environment
- Windows 10
- Python3
Steps
- Install
> pip install pyinstaller
- Edit name.spec file
# -*- mode: python -*- block_cipher = None added_files = [ ('.\\templates', 'templates'), ('.\\static', 'static') ] a = Analysis(['__init__.py'], pathex=['D:\Temp\FlaskTest\flask\examples\tutorial\flaskr'], hiddenimports=['flask'], binaries=[], datas=added_files, hookspath=[], runtime_hooks=[], excludes=[], win_no_prefer_redirects=False, win_private_assemblies=False, cipher=block_cipher) pyz = PYZ(a.pure, a.zipped_data, cipher=block_cipher) exe = EXE(pyz, a.scripts, a.binaries, a.zipfiles, a.datas, name='chartson', debug=False, strip=False, upx=True, console=True , icon='favicon.ico')
- Create installer
> pyinstaller name.spec
- It will create an executable file in a new dist folder under the current directory.
> dist\chartson.exe
Errors
If you execute chartson.exe , it shows an message “ModuleNotFoundError: No module named ‘flask’”. You must append the module name to hiddenimports list. Then delete these folders dist and build and run pyinstaller name.spec
. Or using the command pyinstaller --clean name.spec
.
Another option Pyfladesk
- You can create desktop application by using Flask and QtWebEngine
- Install pyfladesk and additional modules
python3 -m pip install pyfladesk python3 -m pip install routes
- Clone pyfladesk repository
git clone https://github.com/smoqadam/PyFladesk.git
- Change to the example directory
cd PyFladesk\example
- Start the app.
python3 app.py
- Create the installer
pyinstaller -w -F --add-data "templates;templates" --add-data "static;static" app.py
- Start the app. It will open the app then close it automatically. I don’t know why.
dist\app.exe
- I got en error in the event log.
Use pyinstaller to create a console application
- Edit console.py
print('hello world')
- Edit name.spec
# -*- mode: python -*- block_cipher = None added_files = [ ] a = Analysis(['console.py'], pathex=['D:\pyinstaller_console_test'], hiddenimports=[], binaries=[], datas=added_files, hookspath=[], runtime_hooks=[], excludes=[], win_no_prefer_redirects=False, win_private_assemblies=False, cipher=block_cipher) pyz = PYZ(a.pure, a.zipped_data, cipher=block_cipher) exe = EXE(pyz, a.scripts, a.binaries, a.zipfiles, a.datas, name='chartson', debug=False, strip=False, upx=True, console=True , icon='favicon.ico')
- Copy a favicon.ico to the folder
- Create a create_setup.bat and run it. Execute the application with the command
dist\chartson.exe
.pyinstaller name.spec
- Download the example
Use pyinstaller to create a tkinter window application
- Edit window.py
from tkinter import * window = Tk() window.title("Welcome to LikeGeeks app") window.mainloop()
- Create a create_setup.bat.
pyinstaller -w name.spec
- The remaining steps same as Use pyinstaller to create a console application.
- Download the example
Reference
- pyinstaller Using Spec Files
- Pyinstaller打包Python程序以及常见bugs
- pyinstaller打包过程中 no module named “”问题
- PyFladesk
- Create one executable file for a Flask app with PyInstaller
- flask-application-built-using-pyinstaller-not-rendering-index-html
- 用pyinstaller打包python程序
- Python GUI examples (Tkinter Tutorial)
- pyinstaller打包Flask(-)
- 对写的python代码进行加密有什么好的实现方法?
- 使用pyinstaller將python指令碼打包成exe檔案