Page 1 of 1

Python2 vs Python3

Posted: Thursday 25 February 2021 21:44
by RobD
Something is puzzling me.
I have a RPi 4b with the latest version of Raspberry OS running Domoticz 2020.2.
On the Pi, Python2 and Python3 are installed.
I stumbled into a problem because a Python script which was developed in Python3 did not run as a script_time_xxx.py.
Looking into this I discovered that when Domoticz starts a script it is run on Python2 even though I have included the Shebang

Code: Select all

#!/usr/bin/env python3
can anybody help me out?

Re: Python2 vs Python3

Posted: Friday 26 February 2021 17:13
by Thorgal789
On my side have right problem with python, so I run shell script with domoticz, and the shell script run the python script.
It solve lot of issues.

Code: Select all

#!/bin/bash

python3 /home/pi/hp/play_radio.py > /dev/null 2>&1 &

exit 0

Re: Python2 vs Python3

Posted: Saturday 27 February 2021 12:26
by RobD
sure, I have a comparable workaround.
the thing is that I like to understand the cause of the problem

Re: Python2 vs Python3

Posted: Saturday 27 February 2021 13:00
by erem
from the command line

Code: Select all

which python3
my result : /usr/bin/python3

then try changing the shebang to

Code: Select all

#!/usr/bin/python3

Re: Python2 vs Python3

Posted: Saturday 27 February 2021 13:06
by waaren
RobD wrote: Saturday 27 February 2021 12:26 sure, I have a comparable workaround.
the thing is that I like to understand the cause of the problem
The Shebang! line is ignored when the code is parsed as parm to the controlling interpreter
Consider this Python source with a Shebang!

Code: Select all

#!/usr/bin/python3

import sys

print(sys.version)
# Force use of Python3

Code: Select all

python3 ./version.py      
3.7.3 (default, Jul 25 2020, 13:03:44)
[GCC 8.3.0]
# Force use of Python2

Code: Select all

python2 ./version.py  
2.7.16 (default, Oct 10 2019, 22:02:15)
[GCC 8.3.0]
# Relying on Shebang! for choosing the right interpreter

Code: Select all

./version.py    

3.7.3 (default, Jul 25 2020, 13:03:44)
[GCC 8.3.0]