Environment

  • Windows 10
  • Python3

Steps

  1. Install
    > pip install pyinstaller
    
  2. 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')
    
  3. Create installer
    > pyinstaller name.spec
    
  4. 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
    

    PyFladesk hello app

  • 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. PyFladesk event log error

Use pyinstaller to create a console application

  1. Edit console.py
    print('hello world')
    
  2. 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')
    
  3. Copy a favicon.ico to the folder
  4. Create a create_setup.bat and run it. Execute the application with the command dist\chartson.exe.
    pyinstaller name.spec
    
  5. Download the example

Use pyinstaller to create a tkinter window application

  1. Edit window.py
    from tkinter import *
    window = Tk()
    window.title("Welcome to LikeGeeks app")
    window.mainloop()
    
  2. Create a create_setup.bat.
    pyinstaller -w name.spec
    
  3. The remaining steps same as Use pyinstaller to create a console application.
  4. Download the example

Reference