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

Do you have a question? Post it now! No Registration Necessary

Translate This Thread From English to

Threaded View
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

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

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

 > 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.

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


Re: Python script run in Pi desktop terminal, but not by ssh terminal
On 10/11/2020 06:05, Vincent Coen wrote:
Quoted text here. Click to load it

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

Pn script rut run in Pi desktop terminal, but not by ssh terminal
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


 > On 10/11/2020 06:05, Vincent Coen wrote:
 >> Hello RobH!
 >>
 >> Tuesday November 10 2020 17:40, you wrote to All:
 >>
 >>   > 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.
 >>
 >> 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
 >>

 > 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



Vincent


Re: Pn script rut run in Pi desktop terminal, but not by ssh terminal
On 10/11/2020 10:42, Vincent Coen wrote:
Quoted text here. Click to load it

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

Pript rut rut run in Pi desktop terminal, but not by ssh terminal
Hello RobH!

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

 > On 10/11/2020 10:42, Vincent Coen wrote:
 >> 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
 >>
 >>
 >>   > On 10/11/2020 06:05, Vincent Coen wrote:
 >>   >> Hello RobH!
 >>   >>
 >>   >> Tuesday November 10 2020 17:40, you wrote to All:
 >>   >>
 >>   >>   > 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.
 >>   >>
 >>   >> 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
 >>   >>
 >>
 >>   > 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
 >>
 >>
 >>
 >> Vincent
 >>

 > 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


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


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

Quoted text here. Click to load it
    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.


Quoted text here. Click to load it

    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
     snipped-for-privacy@ix.netcom.com    http://wlfraed.microdiversity.freeddns.org/

Re: Python script run in Pi desktop terminal, but not by ssh terminal
On 10/11/2020 20:28, Dennis Lee Bieber wrote:
Quoted text here. Click to load it

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 $



Re: Python script run in Pi desktop terminal, but not by ssh terminal
On 10/11/2020 20:43, RobH wrote:
Quoted text here. Click to load it

Quoted text here. Click to load it

Quoted text here. Click to load it

Quoted text here. Click to load it

Quoted text here. Click to load it

Quoted text here. Click to load it
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 $


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

Quoted text here. Click to load it
    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")

  
Quoted text here. Click to load it

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

Quoted text here. Click to load 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...

Quoted text here. Click to load it

... 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...

Quoted text here. Click to load it
    
    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.
Quoted text here. Click to load it
pi@rpi3bplus-1:~$
=-=-=


--  
    Wulfraed                 Dennis Lee Bieber         AF6VN
     snipped-for-privacy@ix.netcom.com    http://wlfraed.microdiversity.freeddns.org/

Re: Python script run in Pi desktop terminal, but not by ssh terminal
On 11/11/2020 02:17, Dennis Lee Bieber wrote:
Quoted text here. Click to load it

Because I have always done since I started using a Pi.
Quoted text here. Click to load it

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

Quoted text here. Click to load it

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

Re: Python script run in Pi desktop terminal, but not by ssh terminal
Quoted text here. Click to load it
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


Re: Python script run in Pi desktop terminal, but not by ssh terminal
On 10/11/2020 19:38, Chris Green wrote:
Quoted text here. Click to load it

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 <module>
     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


Re: Python script run in Pi desktop terminal, but not by ssh terminal SOLVED
On 10/11/2020 20:22, RobH wrote:
Quoted text here. Click to load it

Quoted text here. Click to load it


Quoted text here. Click to load it

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

Site Timeline