LIFX Developer Zone

New to LIFX and Python development on RPI

Hello. I’m getting started working on a network application that can execute python scrip to control my LIFX devices. I’m familiar with programming in other languages but i wanted to take this opportunity to started with Python and Linux. I’ve been going over the open source API here, and have create a very simple code to get started but have run into my first hurdle. I am able to execute the Python scrips in the examples and lifxlan folders from the Terminal with no problem, but when i try to run my own nothing happens and when i debug them it IDLE 3 i get this error.

from lifxlan import LifxLAN
ImportError: cannot import name ‘LifxLAN’

The weird thing is if i open up an example in IDLE 3 i can a similar error. Could anyone tell me what i’m doing wrong? Here is my code.

#!/usr/bin/env python
# coding=utf-8
import sys

from lifxlan import LifxLAN

def main():
    #number of lights from selector or 1 by default
    num_lights = 1

    # instantiate LifxLAN client, num_lights may be None (unknown).
    # In fact, you don't need to provide LifxLAN with the number of bulbs at all.
    # lifx = LifxLAN() works just as well. Knowing the number of bulbs in advance 
    # simply makes initial bulb discovery faster.
    print("Discovering lights...")
    lifx = LifxLAN(num_lights)

    # get devices
    print("Discovering lights...")
    light1 = lifx.get_lights(num_lights)

    if light1.get_power() == "off":
        print("Light was off.\nNow it's on")
        print("Light was on.\nNow it's off")

    if __name__=="__main__":

Hi, a couple of things to try…

You have an indentation error, last two lines of your code need to be brought out of the main() def.

If that doesn’t work, try making a copy of and making minor modifications, one step at a time.

Yes you were right. That was causing an issue, but i was still getting the error. I did what you said and was able to get it working using IDLE 2.

Just following up on this as i am trying to do the same thing by checking if the light is off if so switch on. I have tried this code and nothing happens code runs no errors but i get no PRINT to screen lights do not power off, weird.

I found this as I also cannot get get_power working on a single device similar thing nothing happens. I can switch on a light by MAC and IP but for some reason i cannot check if the light is on or off. Has anyone got a working version of this code or similar?