问题
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