Preparing Actions for Python 3 Compatibility

CloudBolt is aiming to support Python 3 by version 8.0, and any custom Plug-ins or Actions must also be updated before upgrading. Here is a brief guide to updating your Python code to be compatible with both Python 2 and 3.

One of the easiest tools to automatically check your plugin compatibility is 2to3. Run it on the source files of your Plug-ins, and a diff against the original source file is printed.

Most Plug-ins and Actions won't be using methods or syntax that have significant changes. But there are a few common differences that you should review.

Print statements need to be a method:

Screen_Shot_2017-08-10_at_9.32.20_AM.png

Raising exceptions should also use a method to set a specific error message:

Screen_Shot_2017-08-10_at_9.34.29_AM.png

Catching exceptions need to use 'as' instead of a comma:

Screen_Shot_2017-08-10_at_9.35.35_AM.png 

 

Unicode changes will impact how data is read from external sources like the 'requests' library:

Python 2 Python 3

r = requests.get(‘https://google.com’)

content = r.content()

r = requests.get(‘https://google.com’)

content = r.content.decode()

 

String formatting has changed, and the old % formatting is no longer allowed:

Python 2 Python 3
“%d %s” % (i, s) “{} {}”.format(i, s)
“%d/%d=%f” % (355, 113, 355/113) “{:d}/{:d}={:f}”.format(355, 113, 355/113)

 

Here are a few excellent resources on the topic that can provide more detailed information:

https://docs.djangoproject.com/en/1.11/topics/python3/

https://docs.python.org/3/howto/pyporting.html#pyporting-howto

http://python-future.org/compatible_idioms.html#strings-and-bytes

If you have any trouble updating your code, please review those resource and then contact CloudBolt support at support@cloudboltsoftware.com.

Have more questions? Submit a request

0 Comments

Please sign in to leave a comment.