pfletch101
Posts: 214
Joined: Sat Feb 24, 2018 4:09 am

Installing Program requiring Python 2.7 when default is 3.x

Fri Apr 27, 2018 10:35 pm

I have both (2.x and 3.x) versions of Python installed on my Pi 3B+, but I have it set up to default to 3.x. I want to install Weewx, which requires 2.7, and I know that the install will work if I temporarily make the older version the default, but I suspect that if I subsequently set the default back to 3.x, Weewx (which normally needs to run all the time as a daemon) will get confused. Is there a standard workaround for this sort of problem - a service needing a non-default python version?

User avatar
DougieLawson
Posts: 34128
Joined: Sun Jun 16, 2013 11:19 pm
Location: Basingstoke, UK
Contact: Website

Re: Installing Program requiring Python 2.7 when default is 3.x

Fri Apr 27, 2018 10:39 pm

Python 2.7 is still the default in Raspbian Stretch. You have to jump through a small hoop to run python3.
Microprocessor, Raspberry Pi & Arduino Hacker
Mainframe database troubleshooter
MQTT Evangelist
Twitter: @DougieLawson

2012-18: 1B*5, 2B*2, B+, A+, Z, ZW, 3Bs*3, 3B+

Any DMs sent on Twitter will be answered next month.

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

Re: Installing Program requiring Python 2.7 when default is 3.x

Fri Apr 27, 2018 11:28 pm

pfletch101 wrote:
Fri Apr 27, 2018 10:35 pm
I have both (2.x and 3.x) versions of Python installed on my Pi 3B+, but I have it set up to default to 3.x. I want to install Weewx, which requires 2.7, and I know that the install will work if I temporarily make the older version the default, but I suspect that if I subsequently set the default back to 3.x, Weewx (which normally needs to run all the time as a daemon) will get confused. Is there a standard workaround for this sort of problem - a service needing a non-default python version?
The standard workaround is to leave the default alone and when you need python 3 then start your script like this:

Code: Select all

python3 /path/filename.py
Also use #!/usr/bin/python3 as first line of the program.
Unless specified otherwise my response is based on the latest and fully updated Raspbian Stretch w/ Desktop OS.

pfletch101
Posts: 214
Joined: Sat Feb 24, 2018 4:09 am

Re: Installing Program requiring Python 2.7 when default is 3.x

Sat Apr 28, 2018 12:04 am

DougieLawson wrote:
Fri Apr 27, 2018 10:39 pm
Python 2.7 is still the default in Raspbian Stretch. You have to jump through a small hoop to run python3.
As I noted, I have set my installation up to default to Python 3.x.

pfletch101
Posts: 214
Joined: Sat Feb 24, 2018 4:09 am

Re: Installing Program requiring Python 2.7 when default is 3.x

Sat Apr 28, 2018 12:08 am

klricks wrote:
Fri Apr 27, 2018 11:28 pm
pfletch101 wrote:
Fri Apr 27, 2018 10:35 pm
I have both (2.x and 3.x) versions of Python installed on my Pi 3B+, but I have it set up to default to 3.x. I want to install Weewx, which requires 2.7, and I know that the install will work if I temporarily make the older version the default, but I suspect that if I subsequently set the default back to 3.x, Weewx (which normally needs to run all the time as a daemon) will get confused. Is there a standard workaround for this sort of problem - a service needing a non-default python version?
The standard workaround is to leave the default alone and when you need python 3 then start your script like this:

Code: Select all

python3 /path/filename.py
Also use #!/usr/bin/python3 as first line of the program.
As I noted, my problem is the opposite one - all my scripts either require or are happy with Python 3 except Weewx, which runs as a service, though I may be able to modify the command that (I assume) starts it on boot.

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

Re: Installing Program requiring Python 2.7 when default is 3.x

Sat Apr 28, 2018 12:33 am

pfletch101 wrote:
Sat Apr 28, 2018 12:08 am
.......

As I noted, my problem is the opposite one - all my scripts either require or are happy with Python 3 except Weewx, which runs as a service, though I may be able to modify the command that (I assume) starts it on boot.
Weewx may not be the only application that depends on Python 2 as default.
If you want to keep system compatibility then IMO change the default back to python 2 and use python3 to start Python 3 code and either python or python 2 to start Python 2 code...... and when Python 4 comes out use python4 to start that..........
Read explanation here especially the post at the end: https://askubuntu.com/questions/777085/ ... hon-binary
Unless specified otherwise my response is based on the latest and fully updated Raspbian Stretch w/ Desktop OS.

pfletch101
Posts: 214
Joined: Sat Feb 24, 2018 4:09 am

Re: Installing Program requiring Python 2.7 when default is 3.x

Sat Apr 28, 2018 1:17 am

klricks wrote:
Sat Apr 28, 2018 12:33 am
Weewx may not be the only application that depends on Python 2 as default.
If you want to keep system compatibility then IMO change the default back to python 2 and use python3 to start Python 3 code and either python or python 2 to start Python 2 code...... and when Python 4 comes out use python4 to start that..........
Read explanation here especially the post at the end: https://askubuntu.com/questions/777085/ ... hon-binary
You are probably right, but the longer the default is a deprecated version of Python, the longer people will continue not to bring their scripts up to date. Sigh!!

User avatar
paddyg
Posts: 2227
Joined: Sat Jan 28, 2012 11:57 am
Location: UK

Re: Installing Program requiring Python 2.7 when default is 3.x

Sat Apr 28, 2018 6:59 am

I would side with @pfletch101 on this. In my opinion raspbian should have switched to defaulting to py3 months ago. In only 'a few months' support is going to be dropped for py2.

I've not looked in detail at the source for weewx but I doubt very much that it needs to be py2 only - marshmallow-sqlalchemy and sqlalchemy that it depends on claim to work with py3. why not patch weewx and send a pull request. Or post an issue on github to get the maintainer to do it?
also https://groups.google.com/forum/?hl=en-GB&fromgroups=#!forum/pi3d

B.Goode
Posts: 6542
Joined: Mon Sep 01, 2014 4:03 pm
Location: UK

Re: Installing Program requiring Python 2.7 when default is 3.x

Sat Apr 28, 2018 7:13 am

I know nothing of Weewx and its dependencies, but isn't this just the converse of the problem most people have running Python3 code on a 'default to 2' system? And can't it be solved the same way? Either invoke the script explicitly with Python2, or include a shebang that specifies Python2 as the interpreter and make the script executable so the shell finds the right version.

But maybe that's so simple that I've missed the point...

User avatar
DougieLawson
Posts: 34128
Joined: Sun Jun 16, 2013 11:19 pm
Location: Basingstoke, UK
Contact: Website

Re: Installing Program requiring Python 2.7 when default is 3.x

Sat Apr 28, 2018 8:39 am

pfletch101 wrote:
Sat Apr 28, 2018 12:04 am
DougieLawson wrote:
Fri Apr 27, 2018 10:39 pm
Python 2.7 is still the default in Raspbian Stretch. You have to jump through a small hoop to run python3.
As I noted, I have set my installation up to default to Python 3.x.
That is a mistake that will break things in Raspbian that expect python 2.7 to be the default.
Microprocessor, Raspberry Pi & Arduino Hacker
Mainframe database troubleshooter
MQTT Evangelist
Twitter: @DougieLawson

2012-18: 1B*5, 2B*2, B+, A+, Z, ZW, 3Bs*3, 3B+

Any DMs sent on Twitter will be answered next month.

User avatar
paddyg
Posts: 2227
Joined: Sat Jan 28, 2012 11:57 am
Location: UK

Re: Installing Program requiring Python 2.7 when default is 3.x

Sat Apr 28, 2018 8:44 am

@B.Goode, yes you're right generally but weewx seems to have simplified installing and running http://weewx.com/docs/debian.htm
[I was looking at the wrong page before!! :oops: It needs python-configobj, python-cheetah and python-imaging. I think all those have py3 versions now but it's a long-winded job to revise it all.]

It's probably possible to manually download and run setup.py for weewx using python2 explicitly, there are some instructions for that in the docs http://weewx.com/docs/setup.htm changing the lines to

Code: Select all

python2 setup.py build
sudo python2 setup.py install
also https://groups.google.com/forum/?hl=en-GB&fromgroups=#!forum/pi3d

User avatar
bensimmo
Posts: 3322
Joined: Sun Dec 28, 2014 3:02 pm
Location: East Yorkshire

Re: Installing Program requiring Python 2.7 when default is 3.x

Sat Apr 28, 2018 8:50 am

DougieLawson wrote:
Sat Apr 28, 2018 8:39 am
pfletch101 wrote:
Sat Apr 28, 2018 12:04 am
DougieLawson wrote:
Fri Apr 27, 2018 10:39 pm
Python 2.7 is still the default in Raspbian Stretch. You have to jump through a small hoop to run python3.
As I noted, I have set my installation up to default to Python 3.x.
That is a mistake that will break things in Raspbian that expect python 2.7 to be the default.
Maybe a mistake but there will be errors if it doesn't work.
Jessie may be a problem, but Stretch had Python3 as a lot of it's compatability updates. Ted hopefully get rid of python2 dependencies (not looked to see if that is a plan).

Iirc you just use python2 if using a python2 program or whatever you symbolic link is called.

User avatar
DougieLawson
Posts: 34128
Joined: Sun Jun 16, 2013 11:19 pm
Location: Basingstoke, UK
Contact: Website

Re: Installing Program requiring Python 2.7 when default is 3.x

Sat Apr 28, 2018 9:01 am

On a stock standard version of Stretch if I run the python command (no suffix) I get python 2.7.13. If I want to run python 3.5.3 I have to use python3 if I must run python 2 I'll use python2.

Code: Select all

[email protected]:~ $ python
Python 2.7.13 (default, Nov 24 2017, 17:33:09)
[GCC 6.3.0 20170516] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>>
[email protected]:~ $ python2
Python 2.7.13 (default, Nov 24 2017, 17:33:09)
[GCC 6.3.0 20170516] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>>
[email protected]:~ $ python3
Python 3.5.3 (default, Jan 19 2017, 14:11:04)
[GCC 6.3.0 20170124] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>>
[email protected]:~ $
It's not difficult, but it it NOT sensible to change the link from /usr/bin/python to point to python3, that WILL break things.
Microprocessor, Raspberry Pi & Arduino Hacker
Mainframe database troubleshooter
MQTT Evangelist
Twitter: @DougieLawson

2012-18: 1B*5, 2B*2, B+, A+, Z, ZW, 3Bs*3, 3B+

Any DMs sent on Twitter will be answered next month.

User avatar
paddyg
Posts: 2227
Joined: Sat Jan 28, 2012 11:57 am
Location: UK

Re: Installing Program requiring Python 2.7 when default is 3.x

Sat Apr 28, 2018 9:45 am

Hopefully the powers behind the scenes (Ted?) *do* have a plan and are rushing to get rid of any python2 dependencies so that nothing breaks when python becomes python3 (next year). In the mean time, each week that goes by adds to the problem as people start to learn how to program unaware that python is anything apart from python.
also https://groups.google.com/forum/?hl=en-GB&fromgroups=#!forum/pi3d

User avatar
DougieLawson
Posts: 34128
Joined: Sun Jun 16, 2013 11:19 pm
Location: Basingstoke, UK
Contact: Website

Re: Installing Program requiring Python 2.7 when default is 3.x

Sat Apr 28, 2018 2:09 pm

It's OK. It's all fixed in 1 year, 8 months, 3 days, 7 hours and 50 minutes when python2 retires as everything will work with python3.

https://pythonclock.org

There won't be any problems with that, in just the same way that breaking TSB Bank out from Lloyds Bank wasn't a complete omnishambles.
Microprocessor, Raspberry Pi & Arduino Hacker
Mainframe database troubleshooter
MQTT Evangelist
Twitter: @DougieLawson

2012-18: 1B*5, 2B*2, B+, A+, Z, ZW, 3Bs*3, 3B+

Any DMs sent on Twitter will be answered next month.

User avatar
karrika
Posts: 1046
Joined: Mon Oct 19, 2015 6:21 am
Location: Finland

Re: Installing Program requiring Python 2.7 when default is 3.x

Sat Apr 28, 2018 2:22 pm

I just noticed that the latest Ubuntu 18.04 LTS for x86 only has python3 installed. So slowly the python2 is being phased out.

nairn62
Posts: 1
Joined: Wed Dec 16, 2015 11:48 pm

Re: Installing Program requiring Python 2.7 when default is 3.x

Thu May 17, 2018 1:58 am

You can use the update-alternatives to give priority to installed programs e.g. Python & Python3.

sudo update-alternatives --remove-all pip
sudo update-alternatives --remove-all pip3
sudo update-alternatives --remove-all python
sudo update-alternatives --remove-all python2
sudo update-alternatives --remove-all python3
sudo update-alternatives --install /usr/bin/pip pip /usr/local/bin/pip 20
sudo update-alternatives --install /usr/bin/pip3 pip3 /usr/local/bin/pip3 20
sudo update-alternatives --install /usr/bin/python python /usr/local/bin/python 20
sudo update-alternatives --install /usr/bin/python2 python2 /usr/local/bin/python2 20
sudo update-alternatives --install /usr/bin/python3 python3 /usr/local/bin/python3 20

In the example above, I've compiled both Python2 (2.7.15)& Python3 (3.6.5) using GCC 8.1, and these are installed into /usr/local/bin (also /usr/local/lib). You would just need to map /usr/bin/python as /usr/bin/python2.

For example:

sudo update-alternatives --install /usr/bin/python python /usr/bin/python2.7 20

User avatar
scruss
Posts: 1821
Joined: Sat Jun 09, 2012 12:25 pm
Location: Toronto, ON
Contact: Website

Re: Installing Program requiring Python 2.7 when default is 3.x

Thu May 17, 2018 3:25 am

karrika wrote:
Sat Apr 28, 2018 2:22 pm
I just noticed that the latest Ubuntu 18.04 LTS for x86 only has python3 installed. So slowly the python2 is being phased out.
Not seeing that here:

Code: Select all

$ grep 'VERSION=' /etc/os-release && python --version
VERSION="18.04 LTS (Bionic Beaver)"
Python 2.7.15rc1
‘Remember the Golden Rule of Selling: “Do not resort to violence.”’ — McGlashan.

Return to “Python”