Re-configure azure-cli to use Python3

When you use azure-cli (2.x) you know it uses Python 2 by default. Nevertheless Python 2.7 will not be maintained from January 1, 2020. This means Python 2 will retire soon. You can find some details and a counter for this special day here: https://pythonclock.org/

Don’t worry, Python 3 is here with standard libraries and higher speed. I am sure you will like it. Now you are thinking about whether how you can rewrite your existing Python2 codes. Luckily there are lot of useful tools for this operation:

2to3https://pypi.org/project/2to3
Python Futurehttps://pypi.org/project/future
Modernizehttps://python-modernize.readthedocs.io/en/latest
Sixhttps://pypi.org/project/six
Online converterhttp://www.pythonconverter.com

This is great. Are most of the problems solved? Not really.

How can we check whether azure-cli uses Python 2.7?

The answer is quite simple. Just execute the following command – check version:

az -v 

Check the bottom of result:

azure-cli with Python 2.7.15rc1

This means azure-cli uses Python 2. This is not good news for us if we would like to use Python 3 for the future.

Re-configure azure-cli to use Python 3

Accordingly we should make some configuration steps in azure-cli.

1. Upgrade azure-cli with pip3

Be sure everything will work, so execute the upgrade command for azure-cli on Python 3

sudo pip3 install azure-cli --upgrade

2. Find the location of azure-cli

To find the location of the required file, you merely execute the following command.

# Find location of azure-cli
which az

result:

/usr/local/bin/az

3. Edit “az” file

Simply edit the file and change the Python version inside it.

# Edit az file
sudo vim /usr/local/bin/az

as you can see it uses Python 2 for running

Modify the python -m azure.cli “$@” line to python3 -m azure.cli “$@”

python3 -m azure.cli "$@"

Then save the file.

4. Check python version for azure-cli

Execute the known command again

az -v

Perfect…From this point azure-cli uses Python 3 and you can uninstall the Python 2 (please be careful!). 🙂

Conclusion

To modify the Python version under azure-cli is not a big deal.

Nevertheless you need to be careful when you upgrade it to latest version time by time because it may try to use Python 2 again.

However in some newer Linux releases the Python 2 is not installed so azure-cli will use Python 3 automatically.

Good luck for your new life with Python 3 🙂

Be the first to comment on "Re-configure azure-cli to use Python3"

Leave a comment