Upgrading to Python 3

Back to docs

Whilst we do not support Python 3 there is a way to make python 2.7 feel and act a lot more like Python 3… import the future…

TL;DR

Stick this at the top of your code:

# encoding: utf-8
from __future__ import absolute_import, division, print_function, unicode_literals

Enjoy something more like python 3.

How to get that Python-3 experience

Future is a “real” python module and it contains a-lot of things that are nice like print as a function and true division by default.

With the module you can:

  • Use print as a function i.e.
     print('Hello World!')

    not

    print 'Hello World!'

    , for why this is good see this stack exchange Q&A.

  • The ‘/’ or division operator now will always return floats rather than flooring the value. i.e. 1/2 = 0.5 not 0, in python 2.7 you have to type 1.0/2 to get 0.5
  • There is a list of all of the glory discussed in a PEP here.

So now to add all of this to the program. As the __future__ module is a pseudo module the statement that “imports” it must be the first line of code. Not doing this will give a fatal error. We also can’t use * wild card and must list the parts of the library that we want to import. So to import future add this to the top of your code:

from __future__ import absolute_import, division, print_function, unicode_literals

It should also be noted that we recommend using UTF-8, so save your file using UTF-8 encoding.  Once you have done that add this as the very first line in your python file:

# encoding: utf-8

Save and upload your code to your robot and enjoy the Python-3 Experience™.

Last Updated On December 13, 2017