Python script run in Pi desktop terminal, but not by ssh terminal

Hello RobH!

Tuesday November 10 2020 17:40, you wrote to All:

Who owns the script.py and where is it ?

Remember sudo takes root priv. and the directory so try :

sudo python3 /home/pi/script.py or whater the user name is along with the correct python run command is.

Vincent

Reply to
Vincent Coen
Loading thread data ...

Hello RobH!

Tuesday November 10 2020 20:08, you wrote to me:

Do a ssh into your Pi. Then do pwd ls -la

Now do sudo -s pwd ls -la

and report what you get

Vince

Vincent

Reply to
Vincent Coen

I have a python script to run some leds, and I'm having a problem with it! When I ssh into my pi zero, then sudo python3 python script.py, it doesn't run, no leds are lit. But if I open a terminal in the pi desktop and run the same command line, it works and the leds light up.

Anyone know why this happens.

Thanks

Reply to
RobH

Because you have written some very clever code that differentiates between a local login and an ssh login! :-) :-)

More seriously I can't think of any obvious answer, we need more information, or at least *you* need more information. Put some print statements around the code that turns the LEDs and see if it's reached in bith cases.

--
Chris Green
Reply to
Chris Green

I ssh'd into my pi zero, then I created the script with : sudo nano script.py then copied some other python script into the new script.py. I then ran sudo python3 script.py from within the directory the said script is in, which is the Downloads directory. Nothing happens, no errors, no leds lit.

If I open a terminal window on the pi desktop and run the exact same sudo python3 script.py, it runs and the leds light up

Reply to
RobH

Putting print.pixels.fill((0, 0, 0)) sleep(2) print.pixels.fill((0, 0, 255)) print.pixels.show()

produces this error: File "ledstest.py", line 6, in print.pixels.fill((0, 0, 0)) AttributeError: 'builtin_function_or_method' object has no attribute 'pixels'

But as I said, the script runs perfectly well in a terminal on the pi desktop

Thanks

Reply to
RobH

On Tue, 10 Nov 2020 20:08:09 +0000, RobH declaimed the following:

My first question is: WHY use "sudo". Isn't the regular user account a member of a group that has access to GPIO?

pi@rpi3bplus-1:~$ groups pi adm dialout cdrom sudo audio video plugdev games users input netdev gpio i2c spi

My second would be: Show us the CODE AND the commands you are performing... Don't just "say" what you did -- cut&paste directly from the SSH console into a message.

Again -- show us (cut&paste) the exact command sequence being performed.

However...

pi@rpi3bplus-1:~/Downloads$ groups pi adm dialout cdrom sudo audio video plugdev games users input netdev gpio i2c spi pi@rpi3bplus-1:~/Downloads$ sudo groups root pi@rpi3bplus-1:~/Downloads$

... the root user (sudo) does NOT appear to have access to GPIO, SPI, I2C, etc.

Without seeing the script one can not tell if errors are being swallowed or just being ignored.

--
	Wulfraed                 Dennis Lee Bieber         AF6VN 
	wlfraed@ix.netcom.com    http://wlfraed.microdiversity.freeddns.org/
Reply to
Dennis Lee Bieber

This is the script:

pi@raspberrypi:~/Downloads $ sudo nano ledstest.py

import board import neopixel from time import sleep

pixels = neopixel.NeoPixel(board.D18, 56, brightness =1) pixels.fill((0, 0, 0)) sleep(2) pixels.fill((0, 0, 255)) pixels.show()

pi@raspberrypi:~/Downloads $ sudo python3 ledstest.py pi@raspberrypi:~/Downloads $

Reply to
RobH

pi@raspberrypi:~/Downloads $ groups pi adm dialout cdrom sudo audio video plugdev games users input netdev gpio i2c spi pi@raspberrypi:~/Downloads $ sudo groups root pi@raspberrypi:~/Downloads $

Reply to
RobH

It was the led strip at fault, as I found after I tried another strip which did light up when I ran the script.

Thanks

Reply to
RobH

On Tue, 10 Nov 2020 20:48:51 +0000, RobH declaimed the following:

I'll repeat my previous question: WHY are you using SUDO?

Develop using the default account, and only use SUDO when the system tells you that you don't have privileges to perform some action. (Okay -- it seems Adafruits neopixel library does claim it needs to be run as root; likely due to the need to set up DMA, but editing the script sure doesn't need to be done using "sudo")

That's a pretty long Neopixel strip, if it has 56 pixels on it.

The above fill appears to be a do nothing as you don't issue a .show() {You are filling the pixel array with all-OFF, sleeping 2 seconds, and then...

... filling the array with all-BLUE, then issuing a .show() command to send these to the Neopixel strip. You also do not have anything delaying the program after this .show().}

The Neopixel library has a built-in clean-up method which sets everything back to OFF and .show() it when your "pixels" object is garbage collected, and that occurs immediately after your pixels.show() statement, since the program itself ends at that point.

And to save some traffic...

Of course that fails. print() is a function which takes as argument the item to be printed.

print(pixels.fill((0, 0, 0)))

Note that if the .fill() call does not return any status, this will print

None

Try running in interactive mode (entering each line at the prompt, as shown here. NOTE: I don't have a Neopixel strip attached, I'd have to hook an oscilloscope to D18 to see if anything happens -- more than I want to try at this moment.

=-=-= pi@rpi3bplus-1:~$ sudo python3 Python 3.7.3 (default, Jul 25 2020, 13:03:44) [GCC 8.3.0] on linux Type "help", "copyright", "credits" or "license" for more information.

pi@rpi3bplus-1:~$ =-=-=

--
	Wulfraed                 Dennis Lee Bieber         AF6VN 
	wlfraed@ix.netcom.com    http://wlfraed.microdiversity.freeddns.org/
Reply to
Dennis Lee Bieber

Because I have always done since I started using a Pi.

No, it isn't, that's a short one by comparison to the ones with either

144 or 300 leds

Alright, the script needs some editing or whatever, but it was something I used to see if it would light the leds up without errors, which it did initially.

Anyway as per my post lower down, the problem was the actual led strip and nothing else. I tried a 144 leds strip and it worked fine.

Thanks

Reply to
RobH

Apologies for late reply but here are the results of what you asked:

pi@raspberrypi:~ $ pwd /home/pi pi@raspberrypi:~ $ ls -la total 92 drwxrwxrwx 15 pi pi 4096 Nov 11 12:32 . drwxr-xr-x 3 root root 4096 Aug 20 11:31 ..

-rw--w--w- 1 pi pi 671 Nov 10 20:11 .bash_history

-rw-rw-rw- 1 pi pi 220 Aug 20 11:31 .bash_logout

-rw-rw-rw- 1 pi pi 3523 Aug 20 11:31 .bashrc

-rw-r--r-- 1 root root 364 Nov 8 17:22 blinkatest.py drwxrwxrwx 2 pi pi 4096 Aug 20 11:40 Bookshelf drwxrwxrwx 6 pi pi 4096 Nov 8 17:20 .cache drwx-w--w- 12 pi pi 4096 Nov 10 17:10 .config drwxrwxrwx 2 pi pi 4096 Nov 8 16:40 Desktop drwxrwxrwx 2 pi pi 4096 Nov 8 16:40 Documents drwxrwxrwx 6 pi pi 4096 Nov 10 20:40 Downloads drwx-w--w- 3 pi pi 4096 Aug 20 12:09 .gnupg drwxrwxrwx 5 pi pi 4096 Nov 8 17:20 .local drwxrwxrwx 2 pi pi 4096 Nov 8 16:40 Music drwxrwxrwx 2 pi pi 4096 Nov 8 16:40 Pictures

-rw-rw-rw- 1 pi pi 807 Aug 20 11:31 .profile drwxrwxrwx 2 pi pi 4096 Nov 8 16:40 Public drwxrwxrwx 2 pi pi 4096 Nov 8 16:40 Templates drwxrwxrwx 2 pi pi 4096 Nov 8 16:40 Videos

-rw--w--w- 1 pi pi 56 Nov 11 12:32 .Xauthority

-rw------- 1 pi pi 2493 Nov 11 12:32 .xsession-errors

-rw------- 1 pi pi 2493 Nov 11 09:17 .xsession-errors.old pi@raspberrypi:~ $ sudo -s root@raspberrypi:/home/pi# pwd /home/pi root@raspberrypi:/home/pi# ls -la total 92 drwxrwxrwx 15 pi pi 4096 Nov 11 12:32 . drwxr-xr-x 3 root root 4096 Aug 20 11:31 ..

-rw--w--w- 1 pi pi 671 Nov 10 20:11 .bash_history

-rw-rw-rw- 1 pi pi 220 Aug 20 11:31 .bash_logout

-rw-rw-rw- 1 pi pi 3523 Aug 20 11:31 .bashrc

-rw-r--r-- 1 root root 364 Nov 8 17:22 blinkatest.py drwxrwxrwx 2 pi pi 4096 Aug 20 11:40 Bookshelf drwxrwxrwx 6 pi pi 4096 Nov 8 17:20 .cache drwx-w--w- 12 pi pi 4096 Nov 10 17:10 .config drwxrwxrwx 2 pi pi 4096 Nov 8 16:40 Desktop drwxrwxrwx 2 pi pi 4096 Nov 8 16:40 Documents drwxrwxrwx 6 pi pi 4096 Nov 10 20:40 Downloads drwx-w--w- 3 pi pi 4096 Aug 20 12:09 .gnupg drwxrwxrwx 5 pi pi 4096 Nov 8 17:20 .local drwxrwxrwx 2 pi pi 4096 Nov 8 16:40 Music drwxrwxrwx 2 pi pi 4096 Nov 8 16:40 Pictures

-rw-rw-rw- 1 pi pi 807 Aug 20 11:31 .profile drwxrwxrwx 2 pi pi 4096 Nov 8 16:40 Public drwxrwxrwx 2 pi pi 4096 Nov 8 16:40 Templates drwxrwxrwx 2 pi pi 4096 Nov 8 16:40 Videos

-rw--w--w- 1 pi pi 56 Nov 11 12:32 .Xauthority

-rw------- 1 pi pi 2493 Nov 11 12:32 .xsession-errors

-rw------- 1 pi pi 2493 Nov 11 09:17 .xsession-errors.old root@raspberrypi:/home/pi#

Thanks

Reply to
RobH

Hello RobH!

Friday November 13 2020 09:14, you wrote to me:

Well from the above it appears you auto log in to the default user (pi) which is also the super user However the file blinkatest.py was created by root not user pi.

So try this log in as pi then do :

chown pi: blinkatest.py

Now try running the python script.

Bye the bye on my systems I have myself as a (normal) user i.e., vince without root privledges etc and always log in as me.

If I have to do any root admin processes I do sudo (or su - ) as needed and then exit admin before continuing any thing else.

I recommend you do this even if the Pi will only be used standalone as it is good practice for when you do have a system that talks to the outside world.

Also ensure that your router is correctly set up for security ie., the firewall settings also to help with this set up a FIXED ip address for your computers that connrect to the router and don't use DHCP so start with the first available IP number eg, 192.168.1.1 (yours might be 192.168.0.1).

In any event for the first system use .1 and the next .2 and keep a note of what IP address is used in what system.

Don't bother for portables i.e., mobiles but for at home one's you might do so.

All the above help you connect system to system as you know their addresses where with the default addressing you have to hunt for it and if it gets turned off then on again will have a different IP address every time.

Vincent

Reply to
Vincent Coen

ElectronDepot website is not affiliated with any of the manufacturers or service providers discussed here. All logos and trade names are the property of their respective owners.