Getting Started with Python Programming and Scripting in Linux – Part 1

It has been said (and often required by recruitment agencies) that system administrators need to be proficient in a scripting language. While most of us may be comfortable using Bash (or other shell of our choice) to run command-line scripts, a powerful language such as Python can add several benefits.

Learn Python Programming Scripting in Linux

Learn Python Programming Scripting in Linux

To begin with, Python allows us to access the tools of the command-line environment and to make use of Object Oriented Programming features (more on this later in this article).

On top of it, learning Python can boost your career in the fields of desktop applications and data science.

Being so easy to learn, so vastly used, and having a plethora of ready-to-use modules (external files that contain Python statements), no wonder Python is the preferred language to teach programming to first-year computer science students in the United States.

In this 2-article series we will review the fundamentals of Python in hopes that you will find it useful as a springboard to get you started with programming and as a quick-reference guide afterwards. That said, let’s get started.

Python in Linux

Python versions 2.x and 3.x are usually available in most modern Linux distributions out of the box. You can enter a Python shell by typing python or python3 in your terminal emulator and exit with quit():

$ which python
$ which python3
$ python -v
$ python3 -v
$ python
>>> quit()
$ python3
>>> quit()
Running Python Commands on Linux

Running Python Commands on Linux

If you want to discard Python 2.x and use 3.x instead when you type python, you can modify the corresponding symbolic links as follows:

$ sudo rm /usr/bin/python 
$ cd /usr/bin
$ ln -s python3.2 python # Choose the Python 3.x binary here
Remove Python 2 and Use Python 3

Remove Python 2 and Use Python 3

By the way, it is important to note that although versions 2.x are still used, they are not actively maintained. For that reason, you may want to consider switching to 3.x as indicated above. Since there are some syntax differences between 2.x and 3.x, we will focus on the latter in this series.

Another way you can use Python in Linux is through the IDLE (the Python Integrated Development Environment), a graphical user interface for writing Python code. Before installing it, it is a good idea to perform a search to find out what are the versions available for your distribution:

# aptitude search idle     [Debian and derivatives]
# yum search idle          [CentOS and Fedora]
# dnf search idle          [Fedora 23+ version]

Then, you can install it as follows:

$ sudo aptitude install idle-python3.2    # I'm using Linux Mint 13

Once installed, you will see the following screen after launching the IDLE. While it resembles the Python shell, you can do more with the IDLE than with the shell.

For example, you can:

1. open external files easily (File → Open).

Python Shell

Python Shell

2) copy (Ctrl + C) and paste (Ctrl + V) text, 3) find and replace text, 4) show possible completions (a feature known as Intellisense or Autocompletion in other IDEs), 5) change the font type and size, and much more.

On top of this, you can use the IDLE to create desktop applications.

Since we will not be developing a desktop application in this 2-article series, feel free to choose between the IDLE and the Python shell to follow the examples.

If You Appreciate What We Do Here On TecMint, You Should Consider:

TecMint is the fastest growing and most trusted community site for any kind of Linux Articles, Guides and Books on the web. Millions of people visit TecMint! to search or browse the thousands of published articles available FREELY to all.

If you like what you are reading, please consider buying us a coffee ( or 2 ) as a token of appreciation.

Support Us

We are thankful for your never ending support.

Gabriel Cánepa

Gabriel Cánepa is a GNU/Linux sysadmin and web developer from Villa Mercedes, San Luis, Argentina. He works for a worldwide leading consumer product company and takes great pleasure in using FOSS tools to increase productivity in all areas of his daily work.

Your name can also be listed here. Got a tip? Submit it here to become an TecMint author.

RedHat RHCE and RHCSA Certification Book
Linux Foundation LFCS and LFCE Certification Preparation Guide

You may also like...

12 Responses

  1. Digital Forensic says:

    That’s great ! Can you make a full tutorial on how to learn Python programming from beginning in Linux

  2. Thomas says:

    Please, whatever you do, *do not touch the system Python installation!* This includes modifying symlinks or paths or whatever. Large parts of your operating system depend on a particular Python version in the /usr/bin/ directory, be it 2.x or 3.x. This is why some users in the comments report broken functionality.

    Always, always, always use virtual environments which allow you to use whatever Python and third-party package versions you want in an isolated location. Anything else can seriously screw up your distribution.

  3. Edward Drake says:

    Warning: I don’t know why but wicd stops working if you modify the symlink for python3.x to python, effectively breaking your ethernet / wi-fi.

  4. Pow says:

    Hey Guys,

    I am new Linux SysAdmin and I am going to use Python for scripting language so, I Googled about it before but i did not find anything useful for someone like me.

    would you give me some direction for starting python actually for Linux-system administration?

    thank you

  5. CrustyCurmudgeon says:

    If you want to discard Python 2.x and use 3.x instead when you type python, you can modify the corresponding symbolic links as follows:”

    Be advised this will result in broken packages (Samba and Python libs) in Linux Mint 18.2

  6. Utsav patel says:

    Thanks a lot

  7. Constantin says:

    Nice one!
    I will wait for the update.

    Just as note. As sysadmin never remove python 2. One of the utilities that will break will be yum.

  8. chuckers says:

    It is NOT a good idea to throw away Python 2 if you want to use Ansible. Ansible breaks if you use Python 3

    • @chuckers,
      Please note we never said you SHOULD throw away Python 2. We only mentioned updating the symlinks as a possibility. Please check Part 2 of this series where I clarified that in detail: https://www.tecmint.com/learn-python-programming-to-write-linux-shell-scripts/

      • CrustyCurmudgeon says:

        Seriously Gabriel? That’s like posting “If you’d like to increase space on your hard drive, you can use “rm -rf /” and then saying that you” didn’t actually recommend it.” when someone points out it deleted their entire hard drive.

        Novice Linux users come to sites like this and follow directions; “clarifying” potentially damaging instructions after the fact isn’t very useful. You should, at the very least, insert a clear cautionary statement. Better yet, move this “suggestion” and the “clarification to the suggestion” adjacent to each other AND include a cautionary statement that it can break packages on some common distros.

  9. sangram says:

    awesome

Leave a Reply to Edward Drake Cancel reply

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

This site uses Akismet to reduce spam. Learn how your comment data is processed.