Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

speedtest-cli works in console, but not as script

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

like image 302
Makalone LOgman Avatar asked Jan 28 '23 14:01

Makalone LOgman


1 Answers

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"])
like image 148
mirmo Avatar answered Jan 31 '23 22:01

mirmo