Dehlasoul
Posts: 13
Joined: Tue Sep 05, 2017 7:37 pm

Best way to start script AND tell it to use Python3?

Wed Feb 14, 2018 7:19 am

i currently have a script that i think isnt using python3 as the default interpreter to open my .py file

i use this .sh file and code entered in /etc/rc.local so that it starts up when the pi does but i want to know if its possible to force it to use Python3 for this script

Code: Select all

#!/bin/sh

until /home/pi/usb-scanner/usb_scanner.py; do
    date_time=$(date +"%Y-%m-%d %H:%M:%S")
    echo "$date_time\t[Script]\tScript crashed with exit code $?. respawning...\n" | tee /dev/tty >> errors.log
    sleep 1
done
and in /etc/rc.local

Code: Select all

sleep 1

/home/pi/usb-scanner/usb_scanner.sh&

could i do something like this
in my .sh file ...

Code: Select all

#!/bin/sh

until sudo python3 /home/pi/usb-scanner/usb_scanner.py; do
    date_time=$(date +"%Y-%m-%d %H:%M:%S")
    echo "$date_time\t[Script]\tScript crashed with exit code $?. respawning...\n" | tee /dev/tty >> errors.log
    sleep 1
done
or would that not work? or is there a better way?

I use the .sh file to log an error if the Script wont start,


Should i just scrap the .sh file and run

in /etc/rc.local

Code: Select all

(sleep 10;python3 /home/pi/usb-scanner/usb-scanner.py)&

jahboater
Posts: 5824
Joined: Wed Feb 04, 2015 6:38 pm
Location: West Dorset

Re: Best way to start script AND tell it to use Python3?

Wed Feb 14, 2018 7:34 am

The first line of "usb-scanner.py" could be:

#!/usr/bin/python3

Dehlasoul
Posts: 13
Joined: Tue Sep 05, 2017 7:37 pm

Re: Best way to start script AND tell it to use Python3?

Wed Feb 14, 2018 8:13 am

jahboater wrote:
Wed Feb 14, 2018 7:34 am
The first line of "usb-scanner.py" could be:

#!/usr/bin/python3
it is already

pcmanbob
Posts: 9612
Joined: Fri May 31, 2013 9:28 pm
Location: Mansfield UK

Re: Best way to start script AND tell it to use Python3?

Wed Feb 14, 2018 11:56 am

I have called python programs in scripts before without problem like this

Code: Select all

file="/home/pi/door.txt"
current_time=$(date "+%a-%d.%m.%Y")
if [ -e "$file" ]
then
	echo "$file found $current_time"
	python /home/pi/crash_test.py
	cp /home/pi/door.txt /home/pi/daily-results/$current_time.txt
	rm /home/pi/door.txt -f
	

So I see no reason it should not work for python 3, is there a particular reason to run this with sudo ?

Code: Select all

#!/bin/sh

until python3 /home/pi/usb-scanner/usb_scanner.py; do
    date_time=$(date +"%Y-%m-%d %H:%M:%S")
    echo "$date_time\t[Script]\tScript crashed with exit code $?. respawning...\n" | tee /dev/tty >> errors.log
    sleep 1
done
We want information… information… information........................no information no help
The use of crystal balls & mind reading are not supported

Dehlasoul
Posts: 13
Joined: Tue Sep 05, 2017 7:37 pm

Re: Best way to start script AND tell it to use Python3?

Wed Feb 14, 2018 7:02 pm

pcmanbob wrote:
Wed Feb 14, 2018 11:56 am
I have called python programs in scripts before without problem like this

Code: Select all

file="/home/pi/door.txt"
current_time=$(date "+%a-%d.%m.%Y")
if [ -e "$file" ]
then
	echo "$file found $current_time"
	python /home/pi/crash_test.py
	cp /home/pi/door.txt /home/pi/daily-results/$current_time.txt
	rm /home/pi/door.txt -f
	

So I see no reason it should not work for python 3, is there a particular reason to run this with sudo ?

Code: Select all

#!/bin/sh

until python3 /home/pi/usb-scanner/usb_scanner.py; do
    date_time=$(date +"%Y-%m-%d %H:%M:%S")
    echo "$date_time\t[Script]\tScript crashed with exit code $?. respawning...\n" | tee /dev/tty >> errors.log
    sleep 1
done
Oh ok, but i can totally identify the interpreter before the /path/to/file ?


thats pretty much what i was asking, just so i know it's for sure going to use Python3, for some reason my scripts been crashing for a while now with the old set up to start on boot, but when i i manually call on the program with

'pi@user:~ $ python3 myscript.py


it runs perfectly with little to no down time.

n67
Posts: 938
Joined: Mon Oct 30, 2017 4:55 pm

Re: Best way to start script AND tell it to use Python3?

Wed Feb 14, 2018 7:14 pm

DO NOT invoke a python script like this:

Code: Select all

python whatever.py

or

python3 whatever.py

or

/path/to/python something.py
Rather, use a "shebang" line in the script to tell it what interpreter to use, and then invoke the script simply as:

Code: Select all

/path/to/myscript.py
The whole point of the shebang line is to bake into the script itself what interpreter it is written for.

Invoking it as shown above (in the "DO NOT" section) overrides whatever is in the script, and that can cause problems.

P.S. In case the jargon isn't clear, the "shebang" line is the first line of the script, which will look something like:

#!/usr/bin/python3
"L'enfer, c'est les autres"

G fytc hsqr rum umpbq rm qyw rm rfc kmbq md rfgq dmpsk:

Epmu Sn!

J lnacjrw njbruh-carppnanm vxm rb mnuncrwp vh yxbcb!

pcmanbob
Posts: 9612
Joined: Fri May 31, 2013 9:28 pm
Location: Mansfield UK

Re: Best way to start script AND tell it to use Python3?

Wed Feb 14, 2018 7:52 pm

n67 wrote:
Wed Feb 14, 2018 7:14 pm
DO NOT invoke a python script like this:

Code: Select all

python whatever.py

or

python3 whatever.py

or

/path/to/python something.py
Rather, use a "shebang" line in the script to tell it what interpreter to use, and then invoke the script simply as:

Code: Select all

/path/to/myscript.py
The whole point of the shebang line is to bake into the script itself what interpreter it is written for.

Invoking it as shown above (in the "DO NOT" section) overrides whatever is in the script, and that can cause problems.

P.S. In case the jargon isn't clear, the "shebang" line is the first line of the script, which will look something like:

#!/usr/bin/python3

The problem with running the script the way you suggest is it requires the shebang line to be present and the script to be executable, some thing may new users may not be aware of that's why the most common way people are shown how to run a python script is one of the ways you say they should not use.

There is nothing wrong with using one of those ways you are simple calling the interpreter and then you script , which is exactly what would happen if they used your preferred method.
We want information… information… information........................no information no help
The use of crystal balls & mind reading are not supported

Return to “General discussion”