Search code examples
pythonpython-3.xspeed-test

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


Solution

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