jharris1993
Posts: 32
Joined: Sat Oct 05, 2013 10:26 pm

Determine version of Python within running code?

Thu Oct 17, 2019 7:27 pm

Ref: My posting at https://www.raspberrypi.org/forums/view ... 2&t=254607

Point: Python is a moving target.
Point: Based on the version/sub-version you have installed, syntax for various statements, functions, and features can vary over wide limits, causing code written for one version to be totally incompatible with a different version and/or sub-version.

Which leads to the following Musical Question:
Is it possible, somehow, within the context of a python script, to determine what version it's being run against?

Viz.:

Code: Select all

(some code goes here)
if [version of python] < 3:
     print(version of print compatible with v2.n)
elif [version of python] < 4:
     print(version of print compatible with v3.n)
else:
     print("Unknown version of Python!)
     [exit program]          
- - - - - or - - - - -

Code: Select all

(program code goes here)
if [version of python] < 3
     raw_input(. . . . . .)
elif [version of python] < 4
     input(. . . . . .)
else:
     [throw error, etc.]
(more code here)
Of course, the best way would be to test for the python environment at the beginning of the program, throw an error if necessary, and set a variable/bool to indicate what version you're running under - and use that in your tests.

Any ideas?

Thanks!
Jim "JR"

Some see things as they are, and ask "Why?"
I dream things that never were, and ask "Why Not".

Robert F. Kennedy

“Impossible” is only found in the dictionary of a fool.
Old Chinese Proverb

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

Re: Determine version of Python within running code?

Thu Oct 17, 2019 7:29 pm

Code: Select all

import sys
print (sys.version)
print (sys.version_info)

Code: Select all

#!/usr/bin/python3

import sys

print (sys.version_info[0])
print (sys.version_info[1])
print (sys.version_info[2])
print (sys.version_info[3])
print (sys.version_info[4])

print ("major: ", sys.version_info.major,
" minor: ",
sys.version_info.minor,
" micro: ",
sys.version_info.micro,
" releaselevel: ",
sys.version_info.releaselevel,
" serial: ",
sys.version_info.serial
)
Note: Having anything humorous in your signature is completely banned on this forum. Wear a tin-foil hat and you'll get a ban.

Any DMs sent on Twitter will be answered next month.

This is a doctor free zone.

User avatar
PeterO
Posts: 5085
Joined: Sun Jul 22, 2012 4:14 pm

Re: Determine version of Python within running code?

Thu Oct 17, 2019 7:40 pm

jharris1993 wrote:
Thu Oct 17, 2019 7:27 pm
Ref: My posting at https://www.raspberrypi.org/forums/view ... 2&t=254607
You really ought tyo be using Google to find the answers ... These issues may be new to you, but you're several years behind the rest of us who have been dealing with this for a while now :roll:

PeterO
Discoverer of the PI2 XENON DEATH FLASH!
Interests: C,Python,PIC,Electronics,Ham Radio (G0DZB),1960s British Computers.
"The primary requirement (as we've always seen in your examples) is that the code is readable. " Dougie Lawson

jharris1993
Posts: 32
Joined: Sat Oct 05, 2013 10:26 pm

Re: Determine version of Python within running code?

Thu Oct 17, 2019 8:41 pm

PeterO wrote:
Thu Oct 17, 2019 7:40 pm
You really ought tyo be using Google to find the answers ... These issues may be new to you, but you're several years behind the rest of us who have been dealing with this for a while now :roll:
That assumes that Google has a relevant answer for the Pi and the ancient version of Python we use.

99% of the time I've tried to search for relevant Python information on line, it's been for versions totally different from the one we're using.

Viz.: I spent hours and hours slashing and burning my way through a jungle of totally useless answers trying to find out how to apply the simplest of formatting to a print statement.

Result? I ask here first, as the answers are likely to be more relevant than a blind web search.

Translation: Been there, done that, bought the T-shirt.

P.S.
I'm sorry that I'm so far behind you and I'm trying learn this stuff as rapidly as I can.

Unfortunately, learning any language, even one like Python, is a non-trivial task requiring a substantial amount of time, and everyone needs to start somewhere.

Dougie's answer was much better.
1. No criticism, actual or implied.
2. He gave me the information I needed to get a basic idea of what I needed to know, and what to look for if I needed more help. Which I greatly appreciate.
Jim "JR"

Some see things as they are, and ask "Why?"
I dream things that never were, and ask "Why Not".

Robert F. Kennedy

“Impossible” is only found in the dictionary of a fool.
Old Chinese Proverb

gkreidl
Posts: 6120
Joined: Thu Jan 26, 2012 1:07 pm
Location: Germany

Re: Determine version of Python within running code?

Fri Oct 18, 2019 7:04 am

Get the Python(3) documentation and start reading it (the important chapters) instead of googling around.

The documentation is available in different formats. I prefer the Windows Help File format (chm), which is based on HTML but includes nice index and search functions. You need the xchm reader to use it.

sudo apt-get install xchm
wget https://www.python.org/ftp/python/3.7.5/python375.chm
Last edited by gkreidl on Fri Oct 18, 2019 7:18 am, edited 1 time in total.
Minimal Kiosk Browser (kweb)
Slim, fast webkit browser with support for audio+video+playlists+youtube+pdf+download
Optional fullscreen kiosk mode and command interface for embedded applications
Includes omxplayerGUI, an X front end for omxplayer

User avatar
PeterO
Posts: 5085
Joined: Sun Jul 22, 2012 4:14 pm

Re: Determine version of Python within running code?

Fri Oct 18, 2019 7:17 am

jharris1993 wrote:
Thu Oct 17, 2019 8:41 pm
PeterO wrote:
Thu Oct 17, 2019 7:40 pm
You really ought tyo be using Google to find the answers ... These issues may be new to you, but you're several years behind the rest of us who have been dealing with this for a while now :roll:
That assumes that Google has a relevant answer for the Pi and the ancient version of Python we use.
Why do you think there is anything "Pi Specific" to this ? It's the same issue for all distributoins, though they may be taking slightly differing approaches to the solution. I don't believe Raspian is doing anything different to its parent Debian.

99% of the time I've tried to search for relevant Python information on line, it's been for versions totally different from the one we're using.

Viz.: I spent hours and hours slashing and burning my way through a jungle of totally useless answers trying to find out how to apply the simplest of formatting to a print statement.

Result? I ask here first, as the answers are likely to be more relevant than a blind web search.
So you're not very good at selecting the right search terms. It's a skill worth learning and will get you to better results quicker than having to wait for a forum reply.
Translation: Been there, done that, bought the T-shirt.
No it sounds more like you've not tried very hard to get better search terms....


Dougie's answer was much better.
1. No criticism, actual or implied.
2. He gave me the information I needed to get a basic idea of what I needed to know, and what to look for if I needed more help. Which I greatly appreciate.
You could have gotten the same answer in about 15 seconds using google and searching for "python find running version".
PeterO
Discoverer of the PI2 XENON DEATH FLASH!
Interests: C,Python,PIC,Electronics,Ham Radio (G0DZB),1960s British Computers.
"The primary requirement (as we've always seen in your examples) is that the code is readable. " Dougie Lawson

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

Re: Determine version of Python within running code?

Fri Oct 18, 2019 9:01 am

PeterO wrote:
Fri Oct 18, 2019 7:17 am
You could have gotten the same answer in about 15 seconds using google and searching for "python find running version".
PeterO
Guess how I got my answer.
Note: Having anything humorous in your signature is completely banned on this forum. Wear a tin-foil hat and you'll get a ban.

Any DMs sent on Twitter will be answered next month.

This is a doctor free zone.

User avatar
PeterO
Posts: 5085
Joined: Sun Jul 22, 2012 4:14 pm

Re: Determine version of Python within running code?

Fri Oct 18, 2019 9:21 am

DougieLawson wrote:
Fri Oct 18, 2019 9:01 am
PeterO wrote:
Fri Oct 18, 2019 7:17 am
You could have gotten the same answer in about 15 seconds using google and searching for "python find running version".
PeterO
Guess how I got my answer.
My point exactly ! :lol:
PeterO
Discoverer of the PI2 XENON DEATH FLASH!
Interests: C,Python,PIC,Electronics,Ham Radio (G0DZB),1960s British Computers.
"The primary requirement (as we've always seen in your examples) is that the code is readable. " Dougie Lawson

hippy
Posts: 6104
Joined: Fri Sep 09, 2011 10:34 pm
Location: UK

Re: Determine version of Python within running code?

Fri Oct 18, 2019 9:46 am

jharris1993 wrote:
Thu Oct 17, 2019 8:41 pm
That assumes that Google has a relevant answer for the Pi and the ancient version of Python we use.
With respect to the Python versions available on a Pi - Python version 3.7.3 and 2.7.16 are installed by default for Raspbian Buster and it's your choice which to use.

Neither are "ancient" as both were only released in March this year. Python 2.7.16 is the latest release of Python 2. Python 3.7.4, 3.7.5 and 3.8.0 - the last two released just four days ago - haven't yet made it into the default Raspbian but they can be installed manually if you want them.

You are making this versioning issue appear to be a much bigger problem than it actually is. If you choose to use Python 3 then most code examples will work with it, and a lot of code intended for Python 2 will work just as well 'as is' or with a few minor modifications. A lot of code can be written to work with Python 2 and 3 without the code even being aware of which version it's being run as.

User avatar
jojopi
Posts: 3085
Joined: Tue Oct 11, 2011 8:38 pm

Re: Determine version of Python within running code?

Fri Oct 18, 2019 4:29 pm

jharris1993 wrote:
Thu Oct 17, 2019 7:27 pm
if [version of python] < 3
     raw_input(. . . . . .)
elif [version of python] < 4
     input(. . . . . .)
That is cumbersome, and not really a good solution.

Since Python2's input() is so dangerous and should never be used, the best thing is to redefine it as raw_input at the top of any file that might possibly be run under Python2. To make this compatible with Python3, do it only if raw_input exists:

Code: Select all

if hasattr(__builtins__, "raw_input"):
    input = raw_input
Similarly, write your prints in Python3's function syntax, and if you need to also support Python2, add:

Code: Select all

from __future__ import print_function

Return to “Python”