gpt4 book ai didi

python - 属性错误 : 'module' object has no attribute 'SMBus'

转载 作者:可可西里 更新时间:2023-11-01 10:43:46 29 4
gpt4 key购买 nike

晚上好

我一直致力于与我的硬盘进行 SMBus 通信,并且能够通过一个名为 readwriteeverything 的程序读取值。现在,我想创建自己的程序来在导入 smbus 的 python 中读写寄存器。因此,我决定将我的代码基于此示例:

from smbus import SMBus

bus = SMBus(1) # 0 = /dev/i2c-0 (port I2C0), 1 = /dev/i2c-1 (port I2C1)

DEVICE_ADDRESS = 0x15 #7 bit address (will be left shifted to add the read write bit)
DEVICE_REG_MODE1 = 0x00
bus.write_data(DEVICE_ADDRESS, DEVICE_REG_MODE1)
bus.read_data(DEVICE_ADDRESS)

但是,当我尝试运行单行代码时:

from smbus import SMBus

我得到:

ImportError: cannot import name SMBus

我已经尝试过 python2、python3,安装了很多包,例如 python3-smbus,甚至尝试构建包并安装它(尽管那是基于 Raspberry Pi 指南,而且是在桌面上。)

我在 windows 和 linux 上都试过了,遇到了同样的问题。

如何让这个命令正常工作?

最佳答案

您需要安装SMBus模块。尝试:

pip install smbus-cffi --upgrade

包详情看这里smbus-cffi 0.5.1 details

关于python - 属性错误 : 'module' object has no attribute 'SMBus' ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36802705/

29 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com