Can't make executable file from eel app in python

Question:

When I try to create an executable with auto_py_to_exe it builds fine, but when I run it it throws an error:

 Failed to execute script 'main' due to unhandled exception: 'NoneType' object has no attribute 'write'

 Traceback (most recent call last):
 File "main.py", line 1, in <module>
 File "<frozen importlib._bootstrap>", line 1027, in _find_and_load
 File "<frozen importlib._bootstrap>", line 1006, in _find_and_load_unlocked
 File "<frozen importlib._bootstrap>", line 688, in _load_unlocked
 File "PyInstallerloaderpyimod02_importers.py", line 499, in exec_module
 File "eel__init__.py", line 8, in <module>
 File "<frozen importlib._bootstrap>", line 1027, in _find_and_load
 File "<frozen importlib._bootstrap>", line 1006, in _find_and_load_unlocked
 File "<frozen importlib._bootstrap>", line 688, in _load_unlocked
 File "PyInstallerloaderpyimod02_importers.py", line 499, in exec_module
 File "bottle.py", line 73, in <module>
AttributeError: 'NoneType' object has no attribute 'write'

The ‘command’ auto_py_to_exe runs is:

pyinstaller --noconfirm --onedir --windowed --add-data "web folder path"  "main.py path"

Project folder looks like this:
main.py
web
|-main.html
|-main.css
|-main.js

The files are following:

main.py

import eel

eel.init('./web')
eel.start('./main.html')

main.html

<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <meta http-equiv="X-UA-Compatible" content="IE=edge">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <link rel="stylesheet" href="./main.css">
  <script src="./main.js" defer></script>
  <title>Document</title>
</head>
<body>
  <p class="text">Text</p>
</body>
</html>

main.css

.text {
  color: white;
}

main.js

document.body.style.backgroundColor = '#000'

I also tried to run just the command as following, still the same error:

python -m PyInstaller --onedir --windowed --add-data "C:UsersfusedDesktoptestweb:web" "C:UsersfusedDesktoptestmain.py"
Asked By: crybaby

||

Answers:

It is most likely because you are using --windowed option flag. When you use the --windowed or --noconsole you are telling pyinstaller not to run your program with a console attached to it so pyinstaller sets stderr and stdout to None. One of the libraries you are using is likely writing to sys.stdout for logging.

The solution is to put somewhere close to your program’s entry point you need to explicitly assign stderr and stdout to an object that has the a write method. This can be an actual file that you open if you want to store the output of your program into a file, otherwise you can simply use an buffer from the io module.

for example to use an actual file:

import sys
logfile = open('program_output.txt', 'w')
sys.stdout = logfile
sys.stderr = logfile

Or if you just wanted to use a buffer:

import sys
import io
logfile = io.StringIO()
sys.stdout = logfile
sys.stderr = logfile
Answered By: Alexander