Setting Alternatives Path for Python Command in RHEL 8

alternatives: python to /usr/bin/python3

Red Hat Enterprise Linux 8 comes with support for both Python 2 and Python 3. But neither of them is invoked via running python command – you get “command not found” error.

Default Python Version in RHEL 8

Python 3.6 is the default and primary version of Python in RHEL 8. It may need to be istalled, but in my VirtualBox VM installation of RHEL 8 beta it came preinstalled.

Check Python 3 version

Just type python3 to see which version you have:

[greys@rhel8 ~]$ python3
Python 3.6.6 (default, Oct 16 2018, 01:53:53)
[GCC 8.2.1 20180905 (Red Hat 8.2.1-3)] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>>    

Python Command Not Found

If we type python instead of python3, we’ll get the following error:

[greys@rhel8 ~]$ python
bash: python: command not found…

Set Alternatives Path For Python to Python3

There’s a special alternatives method in Red Hat Linux and CentOS, it allows you to select the primary version of a tool when multiple versions are available. Among other things, alternatives command (must be run as root) can create default paths.

This command configures RHEL to invoke /usr/bin/python3 whenever you run python:

[greys@rhel8 ~]$ sudo alternatives --set python /usr/bin/python3
[sudo] password for greys:

That’s it! If you type python again, you’ll see python3 executed instead of getting an error:

[greys@rhel8 ~]$ python
Python 3.6.6 (default, Oct 16 2018, 01:53:53)
[GCC 8.2.1 20180905 (Red Hat 8.2.1-3)] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>>    

Have fun!




Convert Epoch Time with Python

Convert Unix Epoch with Python

I’m slowly improving my Python skills, mostly by Googling and combining multiple answers to code a solution to my systems administration tasks. Today I decided to write a simpe converter that takes Epoch Time as a parameter and returns you the time and date it corresponds to.

datetime and timezone Modules in Python

Most of the functionality is done using the fromtimestamp function of the datetime module. But because I also like seeing time in UTC, I decided to use timezone module as well.

epoch.py script

#!/Library/Frameworks/Python.framework/Versions/3.6/bin/python3

import sys
from datetime import datetime, timezone

if len(sys.argv)>1:
  print ("This is the Epoch time: ", sys.argv[1])

  try:
      timestamp = int(sys.argv[1])

      if timestamp>0:
        timedate = datetime.fromtimestamp(timestamp)
        timedate_utc = datetime.fromtimestamp(timestamp, timezone.utc)

        print ("Time/date: ", format(timedate))
        print ("Time/date in UTC: ", format(timedate_utc))
  except ValueError:
        print ("Timestamp should be a positive integer, please.")
else:
  print ("Usage: epoch.py <EPOCH-TIMESTAMP>")

FIXME: I’ll revisit this to re-publish script directly from GitHub.

Here’s how you can use the script:

greys@maverick:~/proj/python $ ./epoch.py 1566672346
 This is the Epoch time:  1566672346
 Time/date:  2019-08-24 19:45:46
 Time/date in UTC:  2019-08-24 18:45:46+00:00

I implemented basic checks:

  • script won’t run if no command line parameters are passed
  • an error message will be shown if command line parameter isn’t a number (and therefore can’t be a timestamp)

Do you see anything that should be changed or can be improved? Let me know!

See Also




Practical Programming: An Introduction to Computer Science Using Python

practical-programming-an-ntroduction-to-computer-science-using-python.jpg
Practical Programming – Python

Yet another book on Python programming, I read it back in 2014 and must say – I was really impressed, as it’s provided a whole set of skills that I could immediately apply. In this sense, the Pragmatic Programmers series served me well once again – this guide to Python is very practical. Seems there’s a more recent version of it so I’ll be reading it and updating this page soon!

Check Practical Programming: An Introduction to Computer Science Using Python out!

Getting started with Python

The layout of the book ensures that you are getting foundation topics first.

First, you are shown the very basics of Python: simple math and operating with variables. There is a whole chapter devoted to working with strings, their importance in any programming language is universally high.

Once you are more comfortable, you are introduced to modular approach for coding software with Python: importing functionality from standard modules and defining your own modules.

Thanks to the strict syntax requirements, writing a Python code is a thing of beauty. Functions and loops are self-explanatory in most cases, and writing code encourages you to organise logic into functions and to automatically document interfaces.

Before moving on to lists and flow controls, there’s a little bit about working with objects and methods. There’s even an introduction to testing and coding style – it’s very helpful to sort these things out while you’re still very new to Python.
Lists are an amazingly flexible feature in Python, and chapter on Lists is one of the most useful in the whole book. The true beauty of lists is that they are universal – for instance, you can open a file and then access its content as a Python list with the same common methods.

About halfway into the book, you are given enough info and shown enough examples to write quite complex yet elegant pieces of software.

More useful Python skills

Second half of the book covers more advanced topics and goes into greater level of details.

I especially appreciated the chapters on file processing (lots of good examples) and sets/dictionaries. Both chapters were very useful for the recent automation script I wrote – parsing a mix structure configuration file to produce stats based on certain fields.

The databases chapter, in my view, could have been longer and more detailed, but it still provided a very good introduction and showed all the necessary elements for you to effortlessly integrate your Python software with a database.

If you’re interested in object-oriented programming with Python, this book will give you a concise but relevant introduction. If you decide to learn how to add a graphical user interface to your Python software, there’s a chapter on that too. You are also given a great introduction to algorithms and basic approaches to searching and sorting routines.

In summary, I would say this book is an excellent way to learn basics and get started with Python. Each chapter contains lots of questions and exercises for you to answer and solve. All the concepts are provided with complete and easy to understand examples.

This book should be enough of a guidance to write your very first relatively complex software projects with Python. Will also be a great book if at any stage you feel like refreshing your Python skills.

By this book on Amazon: Practical Programming – Introduction to Python.

See Also