How to convert "Shiny for Python" to ".exe" file using pyinstaller

81 Views Asked by At

I tried to convert python file(using Shiny for python) to exe file using pyinstaller. We proceeded with the process below.

(1) create "main.py" python file

import os
import sys
from shiny._main import main
path = os.path.dirname(os.path.abspath(__file__))
apath = os.path.join(path, "app.py")

# these next two lines are only if you are using Windows OS
drive, apath = os.path.splitdrive(apath)
apath = apath.replace("\\","/")
# 

sys.argv = ['shiny', 'run', apath]
main()

(2) create "app.py" python file

from shiny import App, render, ui
app_ui = ui.page_fluid(
    ui.h2("Hello Shiny!"),
    ui.input_slider("n", "N", 0, 100, 20),
    ui.output_text_verbatim("txt"),
)
def server(input, output, session):
    @output
    @render.text
    def txt():
        return f"n*2 is {input.n() * 2}"
app = App(app_ui, server)

(3) enter command "pyinstaller.exe -w -F main.py" Then, the "main.spec" file created.

(4) modify the "main.spec" as below

# -*- mode: python ; coding: utf-8 -*-

block_cipher = None

ui = [
    (r"C:\Users\user_name\AppData\Local\aipforge\Lib\site-packages\shiny", "shiny"),
    ("app.py", ".")
    ]

a = Analysis(
    ['main.py'],
    pathex=[],
    binaries=[],
    datas = ui,
    hiddenimports=[],
    hookspath=[],
    hooksconfig={},
    runtime_hooks=[],
    excludes=[],
    noarchive=False,
)
pyz = PYZ(a.pure)

exe = EXE(
    pyz,
    a.scripts,
    a.binaries,
    a.datas,
    [],
    name='main',
    debug=False,
    bootloader_ignore_signals=False,
    strip=False,
    upx=True,
    upx_exclude=[],
    runtime_tmpdir=None,
    console=False,
    disable_windowed_traceback=False,
    argv_emulation=False,
    target_arch=None,
    codesign_identity=None,
    entitlements_file=None,
)

Note) the virtual environment has not been executed. I tried, but the app.py bundle file was not created.

(5) enter "pyinstaller.exe .\main.spec" command

(6) run main.exe file

(7) erroe has occurred as below.

Traceback (most recent call last):
  File "main.py", line 13, in <module>
  File "click\core.py", line 1157, in __call__
  File "click\core.py", line 1078, in main
  File "click\core.py", line 1688, in invoke
  File "click\core.py", line 1434, in invoke
  File "click\core.py", line 783, in invoke
  File "shiny\_main.py", line 164, in run
    return run_app(
  File "shiny\_main.py", line 290, in run_app
    app, app_dir = resolve_app(app, app_dir)
  File "shiny\_main.py", line 426, in resolve_app
    sys.stderr.write(f"Error: {module_path} not found\n")
AttributeError: 'NoneType' object has no attribute 'write'

How do i solve it? Please help.....ㅠ___________ㅠ

0

There are 0 best solutions below