打包后的Eel应用执行失败:AttributeError: 'NoneType'对象无write属性
Fix for AttributeError: 'NoneType' object has no attribute 'write' in Eel Bundled EXE
This error happens because when using the --noconsole flag with PyInstaller, the standard output (sys.stdout) and standard error (sys.stderr) streams are set to None. The Bottle framework (used internally by Eel) attempts to write to these streams during initialization, triggering the error.
Here are practical fixes:
1. Redirect stdout/stderr in your main script
Add this code at the very top of your survey.py file, before importing Eel:
import sys import os from io import StringIO # Handle case where stdout/stderr are None (--noconsole mode) if sys.stdout is None: # Option 1: Redirect to a log file for debugging log_dir = os.path.join(os.path.expanduser("~"), "MyAppLogs") os.makedirs(log_dir, exist_ok=True) log_path = os.path.join(log_dir, "app_output.log") sys.stdout = open(log_path, "a", encoding="utf-8") sys.stderr = open(log_path, "a", encoding="utf-8") # Option 2: Use a dummy null stream if you don't need logs # class NullWriter(StringIO): # def write(self, msg): # pass # sys.stdout = NullWriter() # sys.stderr = NullWriter() # Now import Eel and rest of your code import eel # ... rest of your script ...
2. Use a custom PyInstaller spec file
Instead of relying on the python -m eel command, generate and modify a spec file:
- Generate the spec file:
python -m eel survey.py web_folder --onefile --noconsole --spec - Open the generated
survey.specfile, and add this code at the top to handle streams:import os import sys if sys.stdout is None: sys.stdout = sys.stderr = open(os.devnull, 'w') - Rebundle using the spec file:
pyinstaller survey.spec
3. Downgrade Bottle to a compatible version
Newer Bottle versions have known issues with --noconsole mode. Try downgrading to version 0.12.20:
pip install bottle==0.12.20
Rebuild the EXE after downgrading.
Content of the question来源于stack exchange,提问作者Maryam Balti
相关产品推荐
相关产品推荐

