Python up and running

      No Comments on Python up and running

Python has certainly become one of the top languages of the day.  In this post, I want to spend some time to get you up and running with it.  We’ll start with a base install of Python and then walk through an example to introduce some basic Python concepts.  If you’re in infrastructure, particularly networking, then Python is a language you should be putting some time towards learning.  Most of the vendors out there are coming out with some level of Python integration for their products.  Matt Oswalt spends some time in one of his recent posts talking about how important this integration is as well as gives a couple of examples.  Bottom line – all of us in infrastructure should be finding better ways to do things and Python is a good place to start. 

Note: If you’re interested in the future of networking as a whole, check out this other post from Matt Oswalt where he talks about next gen networking skills.  Good stuff.

I always like to start from the beginning so let’s start from absolute scratch.  I’m going to start with a CentOS 7 host that has just been built.  The first thing we need to do is install Python.  By default, my CentOS 7 install has Python 2.75 installed.  We can check the version as shown in the below screenshot…

So Python is already installed, now we just want to make it a little easier to use.  To do this, I’m going to install VIM and make some tweaks to it to make it a little easier.  I’m going to steal some of the VIM config from my previous post on golang…

#Install dependancies and neccessary packages
yum -y install git vim wget

#Pull down Vundle
git clone ~/.vim/bundle/Vundle.vim

#Edit your .vimrc file...
vim ~/.vimrc

#Add this config...
set nocompatible
filetype off
colorscheme molokai
" Vundle config
set rtp+=~/.vim/bundle/Vundle.vim
call vundle#rc()
Plugin 'gmarik/Vundle.vim'
Plugin 'scrooloose/nerdtree.git'
filetype plugin indent on
" Set tabs correctly for Python
set tabstop=4
set shiftwidth=4
set expandtab
" Enable Start and End of line
" Enable syntax highlighting
syntax enable
"Autoindent the next line of code
set autoindent
" Automatch brackets
set showmatch

#Pull down the vim color scheme
mkdir ~/.vim/colors
wget -O ~/.vim/colors/molokai.vim

#Start vim and install the plugins

#Wait for the install to complete and exit VIM

Alright – So now we’re ready to start writing some code.  Let’s create a new folder called ‘python’ and create our first Python program call ‘Hello World”…


Typical examples of your first program just print ‘Hello World’ to the console.  That’s hardly any fun so let’s take a different approach. Let’s print ‘Hello World’ in ASCII art font.  To do that, we’ll use this simple program…

import pyfiglet
f = pyfiglet.Figlet(font='slant')
print f.renderText('Hello World!')

Without much (any) programming knowledge we can tell that we’re importing some type of object, creating an instance of that object, and then printing to the screen a function of that object.  Pretty simple huh?

So let’s put that into our file ‘’ and then try to run the program.  To do that, save the file and then preface the filename you wish to run with ‘python’…

So that didn’t work.  Based on the error message, we can see that Python can’t find a module called ‘pyfiglet’.  Looking at our code again, we can see that this a Python package we tried to import.  So let’s talk about imports quickly. 

Python has the concept of packages and modules.  Modules are really just Python functions, definitions, or statements.  Packages are a series of modules.  It’s not quite that simple since we havent talked about Python namespaces yet, but that’s the general idea.  So when we import something, we’re importing another Python function that we can then reference in our code.  So in our example program, we’re importing a package called ‘pyfiglet’.  In the next line of code, we define an instance of ‘pyfiglet’ specifying the module ‘Figlet’ from the ‘pyfiglet’ package.  Another way to do this would have been to just specify the specific module we wanted on import.  If we did that, the code could look like this…

from pyfiglet import Figlet
f = Figlet(font='slant')
print f.renderText('Hello World!')

Notice how on the second line we now only need to specify the module, not the package. 

So you might now be wondering where these packages ,and their associated modules, come from.  Python itself comes with a set of ‘built-in’ modules.  We can see which modules we have available to us by entering the Python shell…


The list is truncated since it’s huge, but looking through it I don’t see the ‘pyfiglet’ module we were looking for.  The reason we don’t see it is that ‘pyfiglet’ is a non-default module.  Non-default modules can be installed through a tool called PIP (which is a recursive acronym that stands for ‘PIP Installs Python’).  To use PIP, we first have to install it…

Now that’ its installed, we simply install ‘pyfiglet’ by saying…

Now if we go back to the Python shell and look at the list of modules we can now see ‘pyfiglet’ as an available module…

So let’s try running our program again…

Success!  So a very simple program, but we’re off the ground with a Python environment.  Next up, we’re going to tackle a more interesting use case.  Stay tuned!

Leave a Reply

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