6

I am trying to get the download and upload speed in python by using the "speedtest" module, but it gives me that error when I use the module:

AttributeError: module 'speedtest' has no attribute 'Speedtest'.

and I was only declaring the variable, that is my code :

import speedtest

speedtester = speedtest.Speedtest()

The module actually doesn't have the functions for some reason. Please tell me what is wrong with my code I was sure to import the module from the cmd and also the pycharm terminal and still got the same error. Thanks in advance

PXL Desiner
  • 73
  • 1
  • 1
  • 8
  • 4
    Looks like you could have a file called `speedtest.py` in the same directory as your script, which would override the actual module. If that's the case, rename it to something else. Otherwise, if that's not the case, maybe tell us how you installed the module, what version got installed, and what Python version you have. – Random Davis Feb 17 '21 at 21:03
  • @RandomDavis I moved the speedtest.py to the same folder as the script and it worked thanks for helping. – PXL Desiner Feb 17 '21 at 21:15
  • That's the opposite of what I said to do; you got the module and moved it _into_ the same directory as your script? I don't know why you did that, where you got the module's file, or what your issue even was. – Random Davis Feb 17 '21 at 21:19
  • @RandomDavis i got the file in PycharmProjects\pythonProject\venv\Lib\site-packages when it was installed and the issue was that the module itself wasn't working it didn't give me suggestions when I did "speedtest." and it gave me that error I talked about in the question when I tried to type "speedtest.Speedtest()" without the auto complete (suggestions) and the my script's name is different from the module's name so that is why i moved the module to the script directory because they were different names and i didn't have to rename it – PXL Desiner Feb 17 '21 at 21:42

10 Answers10

10

I had the same issue. I was using PyCharm IDE. The issue occurs when you install speedtest using pip install speedtest In order to solve the above mentioned issue, you need to use the following command.

pip install speedtest-cli

But before doing this, uninstall the previous one by using pip uninstall speedtest Screenshot to installation

pyeR_biz
  • 986
  • 12
  • 36
Hassan Shahzad
  • 455
  • 6
  • 14
5

I was getting the same error. Then I google the problem and eventually came here. Later I realised that I have named my python file speedtest.py. I renamed it to something else (which is not the name of any python module) and it works just fine now.

<--Screenshot-->

So make sure this case.

hDmtP
  • 51
  • 1
  • 2
2

If you installed "speedtest" and "speedtest-cli" libraries together then this issue will arise.

So first uninstall "speedtest" library using "pip uninstall speedtest" command.

Then try your code like :

"import speedtest st = speedtest.Speedtest() print(st.download()/1024)"

You will get output

Anil Tank
  • 21
  • 1
1

You should uninstall before speedtest whit the command 'pip uninstall speedtest' . After that, this use this code to find download and upload, speeds and ping:

import speedtest

test = speedtest.Speedtest()

print("Loading server list...")
test.get_servers()
print("Choosing best server...")
best = test.get_best_server()

print(f"Found: {best['host']} located in {best['country']}")

print("Performing download test...")
download_result = test.download()
print("Performing upload test...")
upload_result = test.upload()
ping_result = test.results.ping

print(f"Download speed: {download_result / 1024 / 1024:.2f}Mbit/s")
print(f"Upload speed: {upload_result / 1024 / 1024:.2f}Mbit/s")
print(f"Ping: {ping_result}ms")
  • As it’s currently written, your answer is unclear. Please [edit] to add additional details that will help others understand how this addresses the question asked. You can find more information on how to write good answers [in the help center](/help/how-to-answer). – Community Feb 08 '22 at 02:07
1

i was also facing the same issue , what i have done is : i have uninstall the speedtest and speedtest-cli

pip uninstall speedtest

pip uninstall speedtest-cli

then again install speedtest-cli

pip install speedtest-cli

1

I got the same error. in pytho3 we cannot use the following code directly

import speedtest
wifi = speedtest.Speedtest()

Then it will give the error python 'speedtest' has no attribute 'Speedtest'

Instead of that we can use following lines, it will support for Python3

from speedtest import Speedtest

internet = Speedtest()

download_speed = internet.download()
upload_speed = internet.upload() 

print("Download \t:", download_speed)
print("Upload   \t:", upload_speed)
Gehan Fernando
  • 1,221
  • 13
  • 24
0

The issue got solved after moving the speedtest.py file to the same directory as my script and it worked just fine. So just make sure the file is in the same folder as your python script.

PXL Desiner
  • 73
  • 1
  • 1
  • 8
0

Bumped into the issue and investigated the content of the module... It appears what inside my venv folder I got a module folder "speedtest" with EMPTY init.py file and that is it... Next to the folder was speedtest.py with actual code... So deleting the empty folder/module helped me...

DennisBY
  • 11
  • 2
0

I was facing the same problem,but main problem might be you are installing speedtest library which is not suitable instead use pip install speedtest-cli but before that remember to delete the previous one using pip uninstall speedtest and check the spelling of get_servers which is in the plural form

0

If the problem still persists you could simply delete the current code archive and create another one, probably it could be a trash problem, at least for me it finally worked. And for those is still getting the http error, just put secure = True

Example:

st = speedtest.Speedtest(secure = True);
toyota Supra
  • 3,181
  • 4
  • 15
  • 19