nicegui: Unhandled exception when running executable with no console on Windows

When building an executable with pyinstaller on Windows, there is error: Unhandled exception in script and .exe doesn’t work.

build.py is:

import os
import subprocess
from pathlib import Path
import nicegui

cmd = [
    'pyinstaller',
    'main.py', # your main file with ui.run()
    '--name', 'Test app', # name of your app
    '--onefile',
    '--windowed', # prevent console appearing, only use with ui.run(native=True, ...)
    '--add-data', f'{Path(nicegui.__file__).parent}{os.pathsep}nicegui'       
]
subprocess.call(cmd)

Error is:

Traceback (most recent call last):
  File "logging\__init__.py", line 1113, in emit
  File "tempfile.py", line 483, in func_wrapper
TypeError: a bytes-like object is required, not 'str'

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "main.py", line 529, in <module>
  File "nicegui\run.py", line 122, in run
    globals.server.run()
  File "uvicorn\server.py", line 60, in run
  File "asyncio\runners.py", line 190, in run
  File "asyncio\runners.py", line 118, in run
  File "asyncio\base_events.py", line 653, in run_until_complete
  File "uvicorn\server.py", line 77, in serve
  File "uvicorn\server.py", line 88, in startup
  File "uvicorn\lifespan\on.py", line 58, in startup
  File "logging\__init__.py", line 1518, in error
  File "logging\__init__.py", line 1634, in _log
  File "logging\__init__.py", line 1644, in handle
  File "logging\__init__.py", line 1706, in callHandlers
  File "logging\__init__.py", line 978, in handle
  File "logging\__init__.py", line 1118, in emit
  File "logging\__init__.py", line 1031, in handleError
  File "tempfile.py", line 483, in func_wrapper
TypeError: a bytes-like object is required, not 'str'

In main.py I use:

ui.run(native=True, reload=False)

I get the same error for '--windowed' or '--noconsole'. If I comment out '--windowed' or '--noconsole' from build file, .exe file works.

About this issue

  • Original URL
  • State: closed
  • Created a year ago
  • Reactions: 1
  • Comments: 20 (3 by maintainers)

Commits related to this issue

Most upvoted comments

This is not Nicegui issue but is coming from uvicorn. You’d get the same error if you build a fastapi with pyinstaller and the --windowed flag. It seems uvicorn needs to send console log outputs and will not be able to do so if --windowed takes the console away. Solution is to divert stdout to a file. Add the following at the top of your main.py to get rid of this error:

import sys
sys.stdout = open('logs.txt', 'w') # divert stdout to logs.txt file

@edsase is correct but it’s an annoying issue. An alternative to creating a log file is to create a nullwriter output when in a PyInstaller bundle:

import sys

# Are we running in a PyInstaller bundle
# https://pyinstaller.org/en/stable/runtime-information.html#run-time-information
if getattr(sys, 'frozen', False) and hasattr(sys, '_MEIPASS'):
    class NullOutput(object):
        def write(self, string):
            pass

        def isatty(self):
            return False


    sys.stdout = NullOutput()
    sys.stderr = NullOutput()

before calling ui.run().