Python2 vs Python3

Python and python framework

Moderator: leecollings

Post Reply
User avatar
RobD
Posts: 17
Joined: Friday 21 April 2017 15:17
Target OS: Raspberry Pi / ODroid
Domoticz version: 4.10717
Location: Leiden
Contact:

Python2 vs Python3

Post 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?
Plans without action are a daydream.
Action without plans are a nightmare
Thorgal789
Posts: 815
Joined: Wednesday 15 August 2018 14:38
Target OS: -
Domoticz version:
Contact:

Re: Python2 vs Python3

Post 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
User avatar
RobD
Posts: 17
Joined: Friday 21 April 2017 15:17
Target OS: Raspberry Pi / ODroid
Domoticz version: 4.10717
Location: Leiden
Contact:

Re: Python2 vs Python3

Post by RobD »

sure, I have a comparable workaround.
the thing is that I like to understand the cause of the problem
Plans without action are a daydream.
Action without plans are a nightmare
User avatar
erem
Posts: 230
Joined: Tuesday 27 March 2018 12:11
Target OS: Raspberry Pi / ODroid
Domoticz version: 2021.1
Location: Amsterdam/netherlands
Contact:

Re: Python2 vs Python3

Post 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
Regards,

Rob
User avatar
waaren
Posts: 6028
Joined: Tuesday 03 January 2017 14:18
Target OS: Linux
Domoticz version: Beta
Location: Netherlands
Contact:

Re: Python2 vs Python3

Post 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]
Debian buster, bullseye on RPI-4, Intel NUC.
dz Beta, Z-Wave, RFLink, RFXtrx433e, P1, Youless, Hue, Yeelight, Xiaomi, MQTT
==>> dzVents wiki
Post Reply

Who is online

Users browsing this forum: Bing [Bot] and 1 guest