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

1.9k views Asked by At

Can somebody please help with my code?

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)

I get this error:

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

I think it's missing the command for fix attributes.

1

There are 1 answers

0
Ahmed Dhanani On BEST ANSWER

I am unable to install PYA20 on my machine so i can't test this but, this could work out for you. You cannot append a string to an object. In Python the getattr method allows you to get an object's attribute using a string. Here's how it looks:

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)