Have you ever wondered how to process command line arguments in Python? Yeah, there’s a module for that. It’s called argparse, which is a replacement for optparse. In this article, we’ll be taking a whirlwind tour of this helpful module. Let’s start with something simple!

Getting Started

I have always found the simplest way to explain a coding concept is to show some code. So that’s what we’re going to do. Here’s a super simple example that doesn’t do much of anything:

>>> import argparse
>>> parser = argparse.ArgumentParser(
...         description="A simple argument parser",
...         epilog="This is where you might put example usage"
...     )
>>> parser.print_help()
usage: [-h]
A simple argument parser
optional arguments:
  -h, --help  show this help message and exit
This is where you might put example usage

Here we just import argparse and give it a description and set up a usage section. The idea here is that when you ask the program you are creating for help, it will tell you how to use it. In this case, it prints out a simple description, the default optional arguments (“-h” in this case) and example usage.


The other day, I came across an interesting StackOverflow question where the fellow was trying to figure out how to open a sub-frame only once. Basically he wanted a single instance of the sub-frame (and other sub-frames). After digging around a bit on Google, I found an old thread from the wxPython Google Group that had an interesting approach to doing what was needed.

Basically it required a bit of meta-programming, but it was a fun little exercise that I thought my readers would find interesting. Here’s the code:


I recently completed Episode #10 of the Python 101 Screencast. It’s about the basics of functions. I hope you like it!

I recently came across a StackOverflow question where the fellow was asking how to get wxPython’s RichTextCtrl’s XML data so he could save it to a database. I don’t know much about this control, but after a quick Google search, I found an article from 2008 that gave me the information I needed. I took that example and cut it down to the following example:


Check out my latest video from the Python 101 Screencast. In this episode we learn how to use Python’s file handling capabilities. You will learn how to read, write and append to a file.

I recently recorded the next episode of Python 101. This one is on Exception Handling. I hope you like it:

I finished up another Episode of Python 101. This one is about Python’s comprehension constructs. In this video, I cover list, dict and set comprehensions. I hope you enjoy it:

You can support my Python 101 video project via my IndieGogo crowdfunding campaign, which is ending this week.

I recorded the next episode of my Python 101 screencast today. You can support my crowdfunding campaign here.

The video is about Python’s looping structures. You will also learn how to use the else construct with a loop. Here’s the video:

I recently took on a project where I needed to graph some data on a webpage using data I had queried from a database. Since I love Python, I decided to use it to accomplish this task. I went with Flask for serving the webpage and pygal for creating the graphs. In this tutorial, I will show you how to do that too, but without the database logic. Instead, we’ll get weather data from the Weather Underground and graph that. Let’s get started!



My latest project is turning my book, Python 101, into a Screencast. I have started a Kickstarter to raise funds to help in this endeavor. You can check it out here:

The basic idea is to take each chapter of the book and turn it into a screencast. There are 44 chapters currently that will be turned into mini-videos. I’ve already realized I can add a lot of other items in a screencast that are easier to show than to write about, so there will definitely be additional content. I hope you will join me in this project.


Next Page »