As a newbie to Python on the RPi (or anywhere else for that matter), how do I know what version of Python (2 or 3) I'm writing for without looking up every command or syntax while trying to write a script?

  • 1
    How do you expect other people to know which version of Python you are writing for? – Dmitry Grigoryev Mar 21 '17 at 16:09

how do I know what version of Python (2 or 3) I'm writing for

Well, you should know because it is ultimately your decision! You pick one, you use the syntax of that particular generation, and you invoke the respective interpreter to run your script. If you have both versions installed they can typically be run using either python2 or python3. That's also what you would put in the Shebang directive in the beginning of the script (with full path of course).

Usually there will also be a symlink in path called python which will link to one version (more often than not the Python 3 by now). Just type it and it will print something along these lines and you'll know.

$ python
Python 3.6.0 (default, Jan 16 2017, 09:23:00)
[GCC 6.3.1 20170109] on linux
Type "help", "copyright", "credits" or "license" for more information.

These days and starting programming I would consider it ill-advised to not use Python 3. There might be some libraries not available for 3.6 which are out for 2.7 but that should not make you turn backwards. Go for 3!

  • 3
    "but that should not make you turn backwards" Well, sometimes you have to. If a sensor has a large library built for 2 which doesn't work in 3, re-writing the library isn't the obvious alternative. Sticking to 2 is. Afar from that, yes, if you simply want to write something in a new language, it's usually best to pick 3 over 2. – Mast Mar 20 '17 at 21:55
  • 2
    if python is python3 then you must not be running Debian or a derivative thereof like Ubuntu; you may make note of this – cat Mar 20 '17 at 22:26
  • 1
    @Mast: Any library that is not already in the process of adding Python 3.x support is de facto dead at this point. The PSF is dropping support for Python 2.x entirely in 2020, and I expect most libraries to follow suit shortly thereafter. – Kevin Mar 20 '17 at 23:46
  • 2
    @Kevin If a dead library is the only working one, it's still better than no library. I wasn't familiar with the timetable for dropping 2.x entirely though, thanks for the update :-) – Mast Mar 21 '17 at 7:39
  • The python command on Raspbian defaults to 2.7.9 - and the python3 command opens 3.4.2 -- so for commands I need to know the proper shebang if I want to choose v3 – SDsolar Apr 27 '18 at 3:41

I would probably base it on the library I need to use. I would always try to stick with Python 3 as it is the new version, but if a library I need has not been upgraded/converted, then I'll have to use Python 2. Please see here for a list of Python libraries already converted to 3 and those yet to be.


I begin my scripts with:

#!/usr/bin/env python2


#!/usr/bin/env python3

depending on the version I want to use. I think that it is also the most portable way.


To determine the version of python that you are scripting for, it all boils down to syntax, being that one of the major differences between python 2 and 3 is the syntax. If you are using IDLE to create the script, the IDLE shell should have at the top the version of python your using.


There are too many differences between 2.x and 3.x (some of them extremely subtle) for this to be successful. It is, however, relatively easy to determine which version of Python is being run. (Here's one I prepared earlier, when the script I was writing required Python >= 3.4...)

import sys
# Check the version number. enums were only introduced in Python 3.4

if (sys.version_info.major < 3) or (sys.version_info.major == 3 and 
                               sys.version_info.minor < 4):
    logging.error("Requires Python >= 3.4 to run. Aborting execution.")
    logging.info("Python {}.{} detected".format(sys.version_info.major,

All the above are great answers. Another is that if you are learning from a book or an online reference or tutorial; then the version should match the reference you are using. As you go you will start from the lowest common starting point common to all versions. Based on the assumption that the basics have essentially remained the same from one major version to the other. The interpreter will probably throw an error message at you if something has become obsolete or deprecated (some older method may still be used but you are warned that in future versions will no longer be available).

For example raw_input() (Version 2) has been renamed to input() in Version 3 which came to my attention when using PyCharm Community IDE and a reference book. I found this at Stack Exchange: What's the difference between raw_input() and input() in python3.x?

Essentially if you know of a module/function that is no longer available for Version 3 then drop back to version 2, or find a replacement module for Version 3.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.