AttributeError: 'module' object has no attribute 'pin'

AttributeError: 'module' object has no attribute 'pin'

有人可以帮助我编写代码吗?

from pyA20.gpio import gpio
from pyA20.gpio import port

pins = ["PA7",'PA3','PG6']

gpio.init()

for pin in pins:
    led = port.PA7
    gpio.setcfg(port.pin, gpio.OUTPUT)
    gpio.output(port.pin, 1)

我收到这个错误:

AttributeError: 'module' object has no attribute 'pin'

我认为它缺少修复属性的命令。

我无法在我的机器上安装 PYA20,所以我无法对此进行测试,但是,这可能对你有用。您不能将字符串附加到对象。在 Python 中,getattr 方法允许您使用字符串获取对象的属性。外观如下:

from pyA20.gpio import gpio
from pyA20.gpio import port

pins = ["PA7",'PA3','PG6']

gpio.init()

for pin in pins:
    led = port.PA7
    current_port = getattr(port, pin)
    gpio.setcfg(current_port, gpio.OUTPUT)
    gpio.output(current_port, 1)