[英]speedtest-cli works in console, but not as script
I am trying to use the speedtest-cli api.我正在尝试使用 speedtest-cli api。 Copied part of the code from official wiki (and removed unused stuff):
从官方维基复制部分代码(并删除了未使用的东西):
import speedtest
s = speedtest.Speedtest()
s.get_best_server()
s.download()
In python console I get everything ok:在 python 控制台中,我一切正常:
>>> import speedtest
>>> s = speedtest.Speedtest()
>>> s.get_best_server()
{HIDDEN}
>>> s.download()
37257579.09084724
But when I create .py file and run it I get:但是当我创建 .py 文件并运行它时,我得到:
AttributeError: module 'speedtest' has no attribute 'SpeedTest'
Thanks谢谢
As mentioned in the comments, you have a file with the same name and it is conflicting with the import.正如评论中提到的,您有一个同名的文件,它与导入冲突。 Since you have moved the file, restarting the console should work.
由于您已经移动了文件,重新启动控制台应该可以工作。
The code below will also extract the results into a dictionary and make it possible to access the results.下面的代码还将结果提取到字典中,并可以访问结果。
import speedtest
s = speedtest.Speedtest()
s.get_best_server()
s.download()
s.upload()
res = s.results.dict()
print(res["download"], res["upload"], res["ping"])
Try checking speedtest is imported properly尝试检查 speedtest 是否正确导入
import speedtest
print(dir(speedtest))
it should display properties of speedtest它应该显示速度测试的属性
I faced the same issue because I had installed both speedtest and speedtest-cli.我遇到了同样的问题,因为我同时安装了 speedtest 和 speedtest-cli。 Using
pip uninstall speedtest
worked for me.使用
pip uninstall speedtest
对我pip uninstall speedtest
。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.