问题描述
正如标题所说,我在尝试运行这个简单的 Python 代码时遇到了这个错误:
import speedtest
r = speedtest.Speedtest()
结果
Traceback (most recent call last):
File "c:/Users/XXXX/YYYYYY/VSCode/Poli.py",line 3,in <module>
r = speedtest.Speedtest()
AttributeError: module 'speedtest' has no attribute 'Speedtest'
我已经浏览了在 SE 和网络上可以找到的所有帖子: getting an AttributeError: module 'speedtest' has no attribute 'Speedtest' python 'speedtest' has no attribute 'Speedtest' speedtest module in python speedtest-cli works in console,but not as script https://cloudstack.ninja/pratik-amonkar/how-can-i-fixed-error-of-speedtest-module/
我只能确定在我的项目文件夹中某个地方存在一个 speedtest.py,它把事情搞砸了,但我找不到这个特定的文件。我已经多次重新安装该软件包,更改了文件名并尝试了其他虚拟环境。我怎样才能避免这个错误?
解决方法
如果您使用过 pip install speedtest
,可能这就是导致问题的原因。那是安装了错误的包。
您必须使用 speedtest
卸载软件包 pip uninstall speedtest
,而使用 speedtest-cli
安装 pip install speedtest-cli
。第一行 import speedtest
实际上是导入 speedtest-cli。
*请注意,您不能同时安装两者,因为您的脚本实际上是在尝试修改在您执行 speedtest.py
时创建的文件 pip istall speedtest