[英]Can't make executable file from eel app in python
When I try to create an executable with auto_py_to_exe it builds fine, but when I run it it throws an error:当我尝试使用 auto_py_to_exe 创建可执行文件时,它构建良好,但当我运行它时,它会抛出错误:
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 "PyInstaller\loader\pyimod02_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 "PyInstaller\loader\pyimod02_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: auto_py_to_exe 运行的“命令”是:
pyinstaller --noconfirm --onedir --windowed --add-data "web folder path" "main.py path"
Project folder looks like this:项目文件夹如下所示:
main.py主程序
web web
|-main.html |-main.html
|-main.css |-main.css
|-main.js |-main.js
The files are following:这些文件如下:
main.py主程序
import eel
eel.init('./web')
eel.start('./main.html')
main.html主要.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主要.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:\Users\fused\Desktop\test\web:web" "C:\Users\fused\Desktop\test\main.py"
It is most likely because you are using --windowed
option flag.这很可能是因为您正在使用
--windowed
选项标志。 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
.当您使用
--windowed
或--noconsole
时,您是在告诉 pyinstaller 不要在连接到控制台的情况下运行您的程序,因此 pyinstaller 将stderr
和stdout
设置为None
。 One of the libraries you are using is likely writing to sys.stdout
for logging.您正在使用的库之一可能写入
sys.stdout
以进行日志记录。
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.解决方案是将某个位置放在靠近程序入口点的位置,您需要将
stderr
和stdout
显式分配给具有write
方法的 object。 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.如果您想将程序的 output 存储到文件中,这可以是您打开的实际文件,否则您可以简单地使用
io
模块中的缓冲区。
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
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.