Python3 vs Python2 – Playing nice

Like many of my fellow smoke-makers, ol’ Sopwith has been very reluctant to adopt Python3. In my view, Python2.7 is the most stable and flexible version of Python ever released. It is hard to believe the first version of Python3 was released in 2008. Folks – that was six years ago! The time has come to move to Python3.

In my recent work with the AM2315 humidity sensor, I was forced to use Python3 because the quick2wire library I use only runs in the Python3 environment. So, I wrote a Python3 class that wraps the capabilities of the AM2315 sensor.

A couple of important points about Python3. First, installing Python3 on your computer does nothing to your existing Python2.x installation. Python3 is installed in a completely separate location and runs in a separate environment. This means it is very easy to have both version on your system. If you install Python3 on Windows, it will become the default Python version. You can still use Python2.x but you will have to ensure its PATH is set correctly. On Linux you run your Python3 scripts using python3 from the command prompt.

Second, there are a few things in Python3 that you must understand up front. The biggest gotcha for most people is that the print statement is now a function (print()). This is a very good thing although it may take you some time to internalize this change. Also, all ambiguities about Unicode are gone in Python3. This is a big change. You must now think of strings as ‘text’ and all other data as ‘bytes.’ This is truly ‘elegant’ as they say. Once you understand how this works it makes much more sense.

Sopwith recommends that you write all new Python code in Python3. Whether you want to port your old code to Python3 is up to you.

There are going to be times when you absolutely must run some Python3 code from your Python2.x scripts. I recently helped a colleague do this. He had some legacy Python2 code controlling some Raspberry Pi sensors in his terrarium. He wanted to add the AM2315 humidity sensor to the mix and discovered the interface code only worked in Python3 due to the quick2wire library dependency. Porting the old code was not an option.

To solve this problem, I wrote two sample scripts – one in Python2 and the other in Python3. py27_script.py is written for Python2.7 and represents the legacy script.
am2315_py3.py is written for Python3 and simulates the code needed to read the
humidity sensor.

The py27_script.py executes the am2315_py3.py using the subprocess module. To make
this work, both scripts have to be run from the same location or you have to use
full paths. It is important to understand the py27 script is essentially running this
command – “$python3 am2315_py3.py” and reading the results.

To see the scripts work, just run the py27_script.py like this:
$ python py27_script.py
The current temperature is 25.7C.
The current humidity is 50.2%.

Once you read the scripts you will see that it is possible to call pure Python3 code from the Python2.x world. Is this a hack? Yes. Is it elegant? No. But it works.

You can download the two scripts here:

 

Sopwith

 

 

 

 

 

1 thought on “Python3 vs Python2 – Playing nice

  1. WOW !!!, Thanks… you saved me. I’m 2 days that I’m riding the AM2315 sensor to be used in to python2 environment. I tried at least 6 different solution unsuccessfully.

    Thanks your ‘not elegant’ solution IT’S REALLY WORKS and solved my problem.
    This kind of solution is also useful for other python3 scripts that needs to be used in python2 env.

    Thanks again dear.

Leave a Reply

Your email address will not be published. Required fields are marked *