Pi Pico and "can't import name"Neopixel" "

Hi All
I am trying to run neopixels on RPI Pico and also on Waveshare RP2040 zero.
My end plan is to control pattern and brightness with push buttons.
This first piece of code comes from Paul McWhorter lesson 52 I think and works.
When copied to Thonny it runs without a problem on a Pico and the following line does not bring up an error “pix=neopixel.NeoPixel(Pin(pixPin),pixSize)”. The entire code is next

import neopixel
from machine import Pin
import time

pixPin=0
pixSize=5
pix=neopixel.NeoPixel(Pin(pixPin),pixSize)

red=(255,0,0)
green=(0,255,0)
blue=(0,0,255)
orange=(255,100,0)
white=(255,255,255)
pix[0]=red
pix[4]=orange
pix[1]=white
pix[3]=green
pix[2]=blue

pix.write()

However the following code from the Nerd Cave brings up the error I will show after the code and I have many other pieces of code to which I get exactly the same responce from Thonny, when I try and compile it.
Code from Nerd Cave

# Example showing how functions, that accept tuples of rgb values,
# simplify working with gradients

import time
from neopixel import Neopixel

numpix = 5
strip = Neopixel(numpix, 1, 1, "GRB")
# strip = Neopixel(numpix, 0, 0, "GRBW")

red = (255, 0, 0)
orange = (255, 50, 0)
yellow = (255, 100, 0)
green = (0, 255, 0)
blue = (0, 0, 255)
indigo = (100, 0, 90)
violet = (200, 0, 100)
colors_rgb = [red, orange, yellow, green, blue, indigo, violet]

# same colors as normaln rgb, just 0 added at the end
colors_rgbw = [color+tuple([0]) for color in colors_rgb]
colors_rgbw.append((0, 0, 0, 255))

# uncomment colors_rgbw if you have RGBW strip
colors = colors_rgb
# colors = colors_rgbw


step = round(numpix / len(colors))
current_pixel = 0
strip.brightness(50)

for color1, color2 in zip(colors, colors[1:]):
    strip.set_pixel_line_gradient(current_pixel, current_pixel + step, color1, color2)
    current_pixel += step

strip.set_pixel_line_gradient(current_pixel, numpix - 1, violet, red)

while True:
    strip.rotate_right(1)
    time.sleep(0.042)
    strip.show()

error from Nerd Cave code
“Traceback (most recent call last):
File “”, line 5, in
ImportError: can’t import name Neopixel”

I have modified the the Nerd Cave code as seen below to try and make it similar to the Paul McWhorter code but I still get the same error

# Example showing how functions, that accept tuples of rgb values,
# simplify working with gradients

import time
import neopixel #import Neopixel
from machine import Pin
#import rp2
# NUM_LEDS = 8
pixPin = 1
brightness = 0.5
pixSize = 5
pix = neopixel.Neopixel(Pin(pixPin),pixSize)
#pixSize = neopixel.Neopixel(numpix, 0, 28, "GRB")
# strip = Neopixel(numpsix, 0, 0, "GRBW")

red = (255, 0, 0)
orange = (255, 50, 0)
yellow = (255, 100, 0)
green = (0, 255, 0)
blue = (0, 0, 255)
indigo = (100, 0, 90)
violet = (200, 0, 100)
colors_rgb = [red, orange, yellow, green, blue, indigo, violet]

# same colors as normaln rgb, just 0 added at the end
colors_rgbw = [color+tuple([0]) for color in colors_rgb]
colors_rgbw.append((0, 0, 0, 255))

# uncomment colors_rgbw if you have RGBW strip
colors = colors_rgb
# colors = colors_rgbw


step = round(pixSize / len(colors))
current_pixel = 0
strip.brightness(50)

for color1, color2 in zip(colors, colors[1:]):
    strip.set_pixel_line_gradient(current_pixel, current_pixel + step, color1, color2)
    current_pixel += step

strip.set_pixel_line_gradient(current_pixel, pixSize - 1, violet, red)

while True:
    strip.rotate_right(1)
    time.sleep(0.15)
    strip.show()

I expect the explanation is fairly simple for most , but I sure can’t see it .
Any assistance greatly appreciated
cheers
Nick

Hi @Nicholas193967

Welcome back.

We should run some tests to ensure neopixel is installed and not conflicting with other libs on your device.

How do you typically import libraries on your machine?
Do you use Pip? Pipx? Apt?

1 Like

Can you please confirm that you are getting the same error. It seems more likely that the second version is using a different library and you are getting a different error, possibly when you instantiate the object, as the constructor is different.

Hi Pixmusix
Thanks for your quick reply,
I’m using a windows 11 machine. I normally download in one of 2 ways, "copy paste and then "name.py"or download to downloads folder and then open from there.
However in this instance the Paul McWhorter code I typed in directly by looking at what he wrote in his video and the Nerd cave code I copy/pasted from the github website and then “named.py”
When I fist ran Pauls code on the Pico it was the only code on it and it ran perfectly.As soon as I added and tried to run the Nerd Cave code the Nerd Cave code gave the error but Pauls code still works fine.
The Nerd Cave code requires that I have neopixel.py installed on the Pico but Thonny wont allow me to save it there without closing it on Thonny which is impossible.
I do however have neopixel.py saved on the RP2040 zero but the Nerd Cave code still doesn’t work.
I now have a number of other pieces of code on the Pico and of those Pauls code works as do 2 others.
Thanks

Hi Jeff105671,
The original code from the Nerd Cave as shown above returns this error
“Traceback (most recent call last):
File “”, line 5, in
ImportError: can’t import name Neopixel”

The modified code from the Nerd Cave " 3rd set of code from above returns this error
"Traceback (most recent call last):
File “”, line 12, in
AttributeError: ‘module’ object has no attribute ‘Neopixel’
thanks
Nick

1 Like

That indicates that you are referencing a different library, and that other library has a different constructor. The line for the constructor should be:

pix = neopixel.NeoPixel(Pin(pixPin),pixSize)

However, other functions and labels in the library are likely to be different, and you will have to use the library reference to figure out the corresponding functions. I think this is the library you are using (Pi Python library naming is very confusing).
neopixel — control of WS2812 / NeoPixel LEDs — MicroPython latest documentation

1 Like

pix = neopixel.NeoPixel(Pin(pixPin),pixSize)
This line is already in the 3rd set of code above .
Im not sure I follow. Are you saying this line for the constructor should be in the second or third batch of code above
Thanks

The third, just like it is in the first. The library that was used for that first example is the one that you are now using with that change to the import statement, so the format from that first example is what you should be following.

I started off trying to make minimal changes to a 3rd copy of the Nerd Cave code which incidentally is called colorwave.py .I think I have followed your suggestion correctly but kept coming up with numerous errors and at one stage all LED’s just turned off.I therefore kept modifying and it almost identical to Paul’s code in No1 above, so in trying to follow your explanation I’ve gone around in a circle.In it’s present form it does compile.
See code below

# Example showing how functions, that accept tuples of rgb values,
# simplify working with gradients

import time
import neopixel 
from machine import Pin

pixPin = 0
brightness = 0.5
pixSize = 5
pix=neopixel.NeoPixel(Pin(pixPin),pixSize)
# strip = neopixel.Neopixel(pixSize, 1, 1, "GRB")
# strip = Neopixel(numpix, 0, 0, "GRBW")

red = (255, 0, 0)
orange = (255, 50, 0)
yellow = (255, 100, 0)
green = (0, 255, 0)
blue = (0, 0, 255)
indigo = (100, 0, 90)
violet = (200, 0, 100)
colors_rgb = [red, orange, yellow, green, blue, indigo, violet]

pix[0]=red
pix[4]=orange
pix[1]=indigo
pix[3]=green
pix[2]=blue

pix.write()

# # same colors as normaln rgb, just 0 added at the end
# # colors_rgbw = [color+tuple([0]) for color in colors_rgb]
# # colors_rgbw.append((0, 0, 0, 255))
# 
# # uncomment colors_rgbw if you have RGBW strip
# colors = colors_rgb
# # colors = colors_rgbw
# 
# 
# step = round(numpix / len(colors))
# current_pixel = 0
# #pix.brightness(50)
# 
# # for color1, color2 in zip(colors, colors[1:]):
# #     pix.set_pixel_line_gradient(current_pixel, current_pixel + step, color1, color2)
# #     current_pixel += step
# 
# #pix.set_pixel_line_gradient(current_pixel, numpix - 1, violet, red)
# 
# while True:
#     #pix.rotate_right(1)
#     time.sleep(0.042)
#     pix.write()

So in the commented out code above in most cases the word “pix” has replaced the word “strip”

Thanks

1 Like

It won’t be easy to duplicate that code in the library you are using, and doing so will remove the interesting part of the example. The code is demonstrating how to use a tuple to contain the start and end values for a colour gradient, so that the tuple items can be used in the set_pixel_line_gradient method. This means that a particular colour gradient could be used with any length of strip and appear the same. For your library it appears that there is no equivalent of set_pixel_line_gradient, so you can’t simply convert that code. You could write the code to do it, but that would involve careful calculation of the intermediate colour vales, based on the gradient and the number of pixels - an interesting exercise, but not really related to tuples. If you search for examples for your library you might find someone who has already done it, and that would be a better example to follow.

A simpler version of this example would be to build a gradient for a single colour (eg, (100,0,0) to 200,0,0)) for the number of LEDs in your strip and apply that. The principal is the same, except that the calculation of the gradient does not rely on library functions.

Hi @Nicholas193967

While you have mentioned using the Pico and RP2040 Zero, it may be worth looking into WLED, it is a 0 code required platform, but would require the use of an ESP device rather than the RP2040.

1 Like

Hi Jeff
Thats way to complicated for my brain but thank you
Nick

Hi Dan
I’m having a look at wled on line and it looks like it is only possible using a web browser based system which means the esp device must be connected to wifi and or a computer to work.
I need my devices to be fully portable running off a battery.Please correct me if I’m wrong.
I was sort of thinking that WLED would manufacture the code in the background and then send it to the device… sort of like some of the website building programs around

WLED downloads the selected pattern to the device, so it runs that pattern automatically without being connected to a server. The server connection is only required while configuring it. The process of creating WLED patterns for yourself is complex and unique to WLED, so if the pattern you need isn’t already available then it’s not really usable. Also, there is no exposure to how it’s all done, so you won’t learn anything about controlling LEDS. Much better to start with simple patterns for yourself and gradually build up to what you need.

1 Like

Hey @Nicholas193967

WLED is capable of both working as a network device, but can also be configured so that if there isn’t a network connection it will generate its own Access Point that you can connect to for controlling it.

1 Like

Nick, In your original (working) code you have [quote=“Nicholas193967, post:1, topic:22654”]

neopixel.NeoPixel(Pin(pixPin),pixSize)

[/quote]

Note that the function has a capital ‘P’ in the name(NeoPixel), it is not lower case (Neopixel).
Dave

Dave
Thank you,I will correct it and see what difference
cheers
Nick

You already corrected it by using the line of code I posted 3 days ago. It is correct in your most recent listing.

1 Like

Hi All
Don’t know anything much about Python etc but I can’t help but notice there are 3 versions of “NeoPixel” in the sketch in Post 8.
NeoPixel
Neopixel
neopixel
They can’t be all correct ?? Or could they.
Confusing.
Cheers Bob

1 Like

That’s not Python - it’s part of the definitions in the library. So which one is correct depends on the library that is referenced in the ‘imports’ statement, and even that name isn’t necessarily unique. So they could all be correct, in the right context.

2 Likes