[英]Using speedtest-cli within a Python3 script to measure internet speed - getting error message 'No module named speedtest'
My vacation home loses the internet connection from time to time which means I cannot access that locations' home network remotely.我的度假屋不时失去互联网连接,这意味着我无法远程访问该地点的家庭网络。 I am using a wireless Bell service as my ISP.我使用无线贝尔服务作为我的 ISP。 When I am at the vacation home, I have a program within my home automation system to reboot the Bell PoE device and the Modem.当我在度假屋时,我的家庭自动化系统中有一个程序可以重新启动 Bell PoE 设备和调制解调器。 This generally always resets the network.这通常总是重置网络。
I am trying to use the Github project Python script speedtest-cli to measure the download speed at my vacation home.我正在尝试使用 Github 项目 Python 脚本speedtest-cli来测量我度假屋的下载速度。 I am trying to write my own Python script, on a Raspberry Pi (Buster), to periodically measure the download speed of the internet.我正在尝试在 Raspberry Pi (Buster) 上编写自己的 Python 脚本,以定期测量互联网的下载速度。 My plan is to then send a variable, with the speed, to my home automation system that will trigger a reset of the internet if the speed is too low.我的计划是将一个带有速度的变量发送到我的家庭自动化系统,如果速度太低,它将触发互联网重置。
I downloaded the latest script from Github using the code我使用代码从 Github 下载了最新的脚本
wget -O speedtest-cli https://raw.githubusercontent.com/sivel/speedtest-cli/master/speedtest.py
Then I made the file executable with然后我使文件可执行
chmod +x speedtest-cli
When I run the command speedtest-cli , at the Raspeberry Pi's command line, the script works as expected and I see the download speed.当我在 Raspeberry Pi 的命令行中运行命令speedtest-cli 时,脚本按预期工作,我看到了下载速度。
However, when I try to incorporate the code into a python script I get the error 'No module named 'speedtest'但是,当我尝试将代码合并到 python 脚本中时,我收到错误“No module named 'speedtest'
The simple code that yields this message is产生此消息的简单代码是
import speedtest
I am running the simple script above from the /home/pi/ directory and the executable 'speedtest-cli' is also in that directory我正在 /home/pi/ 目录中运行上面的简单脚本,并且可执行文件“speedtest-cli”也在该目录中
(I know that there are numerous comments that this 'speedtest' project isn't providing accurate results, however from my testing it has been consistently yielding results that are sufficient for my purposes) (我知道有很多评论说这个“速度测试”项目没有提供准确的结果,但是从我的测试来看,它一直产生足以满足我的目的的结果)
Any thought on why this isn't working?关于为什么这不起作用的任何想法?
For using speedtest-cli
via a python script, would require you to install speedtest-cli on your local machine.要通过 python 脚本使用speedtest-cli
,需要您在本地机器上安装 speedtest-cli。
git clone https://github.com/sivel/speedtest-cli.git
cd speedtest-cli
python setup.py install
After that you can use import speedtest
之后,您可以使用import speedtest
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.