speedtest-cli works in console, but not as script

孤街浪徒 提交于 2021-01-21 10:28:47

问题


I am trying to use the 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:

>>> 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:

AttributeError: module 'speedtest' has no attribute 'SpeedTest'

Thanks


回答1:


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"])



回答2:


Try checking speedtest is imported properly

import speedtest
print(dir(speedtest))

it should display properties of speedtest



来源:https://stackoverflow.com/questions/51854444/speedtest-cli-works-in-console-but-not-as-script

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!