首页 > 编程问答 >使用 cx_freeze 时 python 库 speedtest/speedtes-cli 和 tkinter 的问题

使用 cx_freeze 时 python 库 speedtest/speedtes-cli 和 tkinter 的问题

时间:2024-07-28 09:46:27浏览次数:8  
标签:python tkinter stdout cx-freeze

我编写了一个脚本来使用 python/tkinter 和 speedtest 库 (speedtest/speedtest-cli) 执行许多宽带速度测试。当作为普通 python 脚本运行时,该程序按预期工作。我可以使用 cx_freeze 创建一个 exe 文件,它会正常创建所有内容。当我运行 exe 文件时,我得到以下回溯...

Traceback (most recent call last):
File
"c:\@python\@w4itdev\lib\site-packages\speedtest.py",
line 156, in <module>
import_builtin___
ModuleNotFoundError: No module named '__builtin__
During handling of the above exception, another exception occurred:
Traceback (most recent call last):
File
"C:\@python\@w4itdev\Lib\site-packages\cx_Freeze\initscript s_startup__.py", line 141, in run module_init.run(name + "__main__")
File
"C:\@python\@w4itdev\Lib\site-packages\cx_Freeze\initscript
s\console.py", line 25, in run
exec(code, main_globals) File "st.py", line 2, in <module>
File "c:\@python\@w4itdev\lib\site-packages\speedtest.py", line 179, in <module>
_py3_utf8_stdout= _Py3Utf8Output(sys.stdout)
File "c:\@python\@w4itdev\lib\site-packages\speedtest.py", line 166, in __init__
buf FilelO(f.fileno), 'w')
AttributeError: 'NoneType' object has no attribute 'fileno'

我已经获取了一个更简单的 python 脚本,该脚本执行类似的工作来测试我的代码是否犯了错误。这给出了相同的结果。为了简单起见,我包含了简单的脚本,而不是我原来的脚本。

from tkinter import *
from speedtest import Speedtest

def update_text():
    speed_test = Speedtest()
    download = speed_test.download()
    upload = speed_test.upload()
    download_speed = round(download / (10**6), 2)
    upload_speed = round(upload / (10**6), 2)
    down_label.config(text= "Download Speed - " + str(download_speed) + "Mbps") 
    up_label.config(text= "Upload Speed - " + str(upload_speed) + "Mbps") 


root = Tk()
root.title("Internet Speed Tracker")
root.geometry('300x300')
button = Button(root, text="Get Speed", width=30, command=update_text)
button.pack()
down_label = Label(root, text="")
down_label.pack()
up_label = Label(root, text="")
up_label.pack()

root.mainloop()

虽然我不是专家,但它似乎是 tkinter/speedtest/cx_freeze 和最终 stdout 的组合。

BTW .. chatgpt 一直在兜圈子试图解决这个问题。


The problem stems from how cx_Freeze handles standard input/output streams (stdin, stdout) when creating an executable. The speedtest library, particularly the _Py3Utf8Output class used for ensuring UTF-8 output, attempts to access the fileno() method of sys.stdout . However, cx_Freeze might redirect or nullify these streams in the frozen executable, leading to the AttributeError: 'NoneType' object has no attribute 'fileno' .

Here are the common solutions to address this issue:

1. Redirect stdout in your script:

You can explicitly redirect stdout to a file before running your main code. This way, speedtest will have a valid file descriptor to work with.

```python import sys import os

if sys.stdout is None: # Check if stdout is None sys.stdout = open(os.devnull, 'w')

from tkinter import * from speedtest import Speedtest

# ... rest of your code ... ```

2. Monkey patch sys.stdout :

Before importing speedtest, you can monkey patch sys.stdout with an object that has a fileno() method.

```python import sys

class FakeStdOut: def fileno(self): return 0

if sys.platform.startswith("win"): sys.stdout = FakeStdOut()

from tkinter import * from speedtest import Speedtest

# ... rest of your code ... ```

3. Use hiddenimports in cx_Freeze setup:

Sometimes, explicitly specifying hidden imports in your cx_Freeze setup script can resolve such issues. This helps the freezer bundle necessary modules that might be missed.

```python from cx_Freeze import setup, Executable

setup( name="MySpeedTestApp", version="0.1", description="Speedtest app", options={ "build_exe": { "packages": ["tkinter", "speedtest"], "includes": ["tkinter"], "hiddenimports": ["_io"], # Add other potentially missing modules if needed } }, executables=[Executable("your_script.py")] ) ```

Explanation:

  • Redirecting stdout ensures that the output stream is pointed to a valid file, preventing the error.
  • Monkey patching replaces the problematic sys.stdout with a custom object that fulfills the fileno() requirement.
  • Using hiddenimports in the cx_Freeze setup script forces the inclusion of the specified modules, which might be indirectly required by the used libraries.

Try these solutions and see if any of them resolve the issue in your cx_Freeze executable. If the problem persists, providing the complete cx_Freeze setup script and relevant code snippets will help in providing more specific guidance.

标签:python,tkinter,stdout,cx-freeze
From: 78802814

相关文章

  • Codeforces Round 962 (Div. 3) A - D详细题解(思路加代码Python,C++(垃圾灰名小白想
             吐槽一下,这次比赛不知道怎么的,可能是div3参加的人比较多吗,代码题解上去后全是inqueue,比赛的过程中我还看了提交的,80多页几千个提交全是inqueue,我的代码等了**半个多小时才运行,然后发现timelimit真的有点搞心态,思路在下一题我还要反过来去优化上一题,不过......
  • python2
    第三方IDE(集成开发工具)   pycharm安装教程    ......
  • 基于python+flask+mysql徐州市天气信息可视化分析系统04600-计算机毕业设计项目选题推
    摘 要信息化社会内需要与之针对性的信息获取途径,但是途径的扩展基本上为人们所努力的方向,由于站在的角度存在偏差,人们经常能够获得不同类型信息,这也是技术最为难以攻克的课题。针对天气信息等问题,对天气信息进行研究分析,然后开发设计出天气信息可视化分析系统以解决问题。......
  • 【免费领源码】Java/Mysql数据库+SSM校园兼职网站 25557,计算机毕业设计项目推荐上万套
    摘 要当今人类社会已经进入信息全球化和全球信息化、网络化的高速发展阶段。丰富的网络信息已经成为人们工作、生活、学习中不可缺少的一部分。人们正在逐步适应和习惯于网上贸易、网上购物、网上支付、网上服务和网上娱乐等活动,人类的许多社会活动正在向网络化发展。兼职......
  • 【免费领源码】Java/Mysql数据库+springboot驾校预约管理系统 25540,计算机毕业设计项
    摘 要随着科学技术的飞速发展,各行各业都在努力与现代先进技术接轨,通过科技手段提高自身的优势;对于驾校预约管理系统当然也不能排除在外,随着网络技术的不断成熟,带动了驾校预约管理系统,它彻底改变了过去传统的管理方式,不仅使服务管理难度变低了,还提升了管理的灵活性。这种......
  • Python Beautiful Soup 不加载表值
    我是美丽汤的新手,不确定如何从该网站为每个州(新南威尔士州、维多利亚州、昆士兰州、南澳大利亚州)添加“解决”栏:https://www.asxenergy.com.au/futures_au似乎没有显示数值数据。我的起始代码是:frombs4importBeautifulSoupfromurllib.requestimportur......
  • c语言模拟Python的命名参数
    最近在书里看到的,让c语言去模拟其他语言里有的命名函数参数。觉得比较有意思所以记录一下。目标众所周知c语言里是没有命名函数参数这种东西的,形式参数虽然有自己的名字,但传递的时候并不能通过这个名字来指定参数的值。而支持命名参数的语言,比如python里,我们能让代码达到这种效......
  • 乌尔都语 Tts 可与 python 一起使用
    我想为乌尔都语创建TTS有什么帮助吗?我发现很少有模特拥抱着脸TheUpperCaseGuy/Guy-Urdu-TTSpocketmonkey/speecht5_tts_urduTalha185/speecht5_finetuned_urdu_TTS但我无法从文本创建或生成高质量的语音任何人都可以帮忙吗???importtorchfromtransformersimp......
  • 有没有办法在Python中使用OpenCV在边界框后面画一条线?
    我正在为我的本科论文编写一个应用程序,该应用程序从根本上允许使用OpenCV库跟踪视频源中的多个对象。为了进一步发展这个想法,我希望能够在屏幕上画一条线,显示边界框在我正在跟踪的对象周围的历史记录。我注意到没有排序用于执行此操作的内置函数,因此任何有关制作此类内容的......
  • 如何向 python pandas 数据透视表添加过滤器? (pd.read_excel)
    我正在尝试使用pandas库在python中重新创建我在Excel中制作的数据透视表。我试图用时间段作为过滤器来总结超过500k行的OD总行程数据。在Excel上,我只会执行行(O)、列(D)、值(行程)、过滤器(时间)。到目前为止,在python上我只有索引、列、值、aggfunc,但我不知道如何过滤。有......