TamP
Posts: 122
Joined: Mon Feb 19, 2018 1:28 pm

Python script from sh

Sat Jun 19, 2021 11:55 am

Both files reside in /home/pi/Documents folder


Code: Select all

#!/bin/sh
cd /
cd home/pi/Documents
sudo python TestSpeech.py
cd 
The python script runs ok on its own

Code: Select all

import pyttsx3
engine = pyttsx3.init()
engine.say("I am talking now ");
engine.setProperty('rate',100)  
engine.runAndWait();
When calling the python script from sh it returns the following error

Code: Select all

  File "TestSpeech.py", line 1, in <module>
    import pyttsx3
ImportError: No module named pyttsx3
Python 3 on Pi400
any help appreciated

GlowInTheDark
Posts: 1834
Joined: Sat Nov 09, 2019 12:14 pm

Re: Python script from sh

Sat Jun 19, 2021 12:12 pm

Lose the "sudo".

sudo does not mean please.
Poster of inconvenient truths.

Linux zealot and proud of it.

TamP
Posts: 122
Joined: Mon Feb 19, 2018 1:28 pm

Re: Python script from sh

Sat Jun 19, 2021 12:37 pm

Removing sudo made no difference
I still get the same error

klricks
Posts: 7858
Joined: Sat Jan 12, 2013 3:01 am
Location: Grants Pass, OR, USA
Contact: Website

Re: Python script from sh

Sat Jun 19, 2021 12:44 pm

Python 3 code needs to be started with python3 not python. (python means Python 2)
Why the redundant CD?

The script can be reduced to:

Code: Select all

#!/bin/bash
python3 /home/pi/Documents/TestSpeech.py
Unless specified otherwise my response is based on the latest and fully updated RPiOS Buster w/ Desktop OS.

GlowInTheDark
Posts: 1834
Joined: Sat Nov 09, 2019 12:14 pm

Re: Python script from sh

Sat Jun 19, 2021 1:25 pm

The script can and should be reduced to:

Code: Select all

$ cd
$ mkdir bin
$ mv Documents/TestSpeech.py bin

Reboot Pi (See below at *), then:

Code: Select all

$ TestSpeech.py

No need for any shell script at all.

Foonote: *) Yes, rebooting is overkill, but let's keep things simple for teaching purposes.
Poster of inconvenient truths.

Linux zealot and proud of it.

GlowInTheDark
Posts: 1834
Joined: Sat Nov 09, 2019 12:14 pm

Re: Python script from sh

Sat Jun 19, 2021 1:27 pm

TamP wrote:
Sat Jun 19, 2021 12:37 pm
Removing sudo made no difference
I still get the same error
Even so, you should remove it - for these reasons:

1) It can mess things up. You will end up with files owned by root instead of being owned by you (user pi).

2) Even if you fix the python2/python3 issue (as suggested by another poster), you will then find that removing the sudo is necessary, since Python seems to keep track of which modules are installed on a per-user basis.
Poster of inconvenient truths.

Linux zealot and proud of it.

TamP
Posts: 122
Joined: Mon Feb 19, 2018 1:28 pm

Re: Python script from sh

Sat Jun 19, 2021 2:15 pm

Should it be
mv Documents/TestSpeech.py bin
or
mv Documents/TestSpeech.py /bin

GlowInTheDark
Posts: 1834
Joined: Sat Nov 09, 2019 12:14 pm

Re: Python script from sh

Sat Jun 19, 2021 2:48 pm

TamP wrote:
Sat Jun 19, 2021 2:15 pm
Should it be
mv Documents/TestSpeech.py bin
or
mv Documents/TestSpeech.py /bin
The first one. As I wrote it.
Poster of inconvenient truths.

Linux zealot and proud of it.

katesimon123
Posts: 53
Joined: Sun Jun 13, 2021 2:45 am

Re: Python script from sh

Sun Jun 20, 2021 6:36 am

TamP wrote:
Sat Jun 19, 2021 2:15 pm
Should it be
mv Documents/TestSpeech.py bin
or
mv Documents/TestSpeech.py /bin
It should be: mv Documents/TestSpeech.py bin

User avatar
thagrol
Posts: 5269
Joined: Fri Jan 13, 2012 4:41 pm
Location: Darkest Somerset, UK
Contact: Website

Re: Python script from sh

Sun Jun 20, 2021 11:51 am

GlowInTheDark wrote:
Sat Jun 19, 2021 1:25 pm
The script can and should be reduced to:

Code: Select all

$ cd
$ mkdir bin
$ mv Documents/TestSpeech.py bin

Reboot Pi (See below at *), then:

Code: Select all

$ TestSpeech.py

No need for any shell script at all.

Foonote: *) Yes, rebooting is overkill, but let's keep things simple for teaching purposes.
You forgot a couple of steps:
  1. Make sure the first line of the script is

    Code: Select all

    #!/usr/bin/env python3
  2. Grant it execute permission:

    Code: Select all

    chmod +x ~/bin/TestSpeech.py[
I'm a volunteer. Take me for granted or abuse my support and I will walk away

All advice given is based on my experience. it worked for me, it may not work for you.
Need help? https://github.com/thagrol/Guides

GlowInTheDark
Posts: 1834
Joined: Sat Nov 09, 2019 12:14 pm

Re: Python script from sh

Sun Jun 20, 2021 12:12 pm

I also left out:

1) Plug the computer in.

2) Open up a terminal window (or log in on a VC).


Probably a few hundred other steps I left out as well.

I'll try to do better in future.
Poster of inconvenient truths.

Linux zealot and proud of it.

TamP
Posts: 122
Joined: Mon Feb 19, 2018 1:28 pm

Re: Python script from sh

Sun Jun 20, 2021 12:54 pm

To recap
My file TestSpeech.py resides in /home/pi/bin

The pyhon script contains the shebang
#!/usr/bin/env python3

If I run ./TestSpeech.py from the /home/pi/bin folder it works

How do I now get it to run when the pi boots ?

User avatar
thagrol
Posts: 5269
Joined: Fri Jan 13, 2012 4:41 pm
Location: Darkest Somerset, UK
Contact: Website

Re: Python script from sh

Sun Jun 20, 2021 12:55 pm

GlowInTheDark wrote:
Sun Jun 20, 2021 12:12 pm
I also left out:

1) Plug the computer in.

2) Open up a terminal window (or log in on a VC).


Probably a few hundred other steps I left out as well.

I'll try to do better in future.
Well yeah. But that wasn't the point I was trying to make. OP is clearly a beginner. Had they followed your instructions they wouldn't have been able to run their python script as you suggested.

Guess I could have phrased that post better. Sorry.
I'm a volunteer. Take me for granted or abuse my support and I will walk away

All advice given is based on my experience. it worked for me, it may not work for you.
Need help? https://github.com/thagrol/Guides

User avatar
thagrol
Posts: 5269
Joined: Fri Jan 13, 2012 4:41 pm
Location: Darkest Somerset, UK
Contact: Website

Re: Python script from sh

Sun Jun 20, 2021 1:01 pm

TamP wrote:
Sun Jun 20, 2021 12:54 pm
To recap
My file TestSpeech.py resides in /home/pi/bin

The pyhon script contains the shebang
#!/usr/bin/env python3

If I run ./TestSpeech.py from the /home/pi/bin folder it works

How do I now get it to run when the pi boots ?
Best way? A systemd service (seach the forums and/or web)
Second best way? cron:
  1. Code: Select all

    crontab -e
  2. Add the following at the end of the file:

    Code: Select all

    @reboot /home/pi/bin/TestSpeech.py >/home/pi/TestSpeech.log 2>&1
Least best way? /etc/rc.local
Add this above exit 0

Code: Select all

su pi /home/pi/bin/TestSpeech.py >/home/pi/TestSpeech.log 2>&1
The output redirection will help should you need to troubleshoot.
I'm a volunteer. Take me for granted or abuse my support and I will walk away

All advice given is based on my experience. it worked for me, it may not work for you.
Need help? https://github.com/thagrol/Guides

TamP
Posts: 122
Joined: Mon Feb 19, 2018 1:28 pm

Re: Python script from sh

Sun Jun 20, 2021 2:19 pm

I thank you for your time and patience
I used the crontab option and all is working well
Thanks again

Return to “Python”