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.


This week we welcome Michael Fogleman as our PyDev of the Week! Mr. Foglebird has been helping out Python programmers prodigiously on StackOverflow for quite some time. I know I’ve appreciated some of his answers when I’ve gone to look for help. He has a large list of personal programming projects on his website that are well worth a look through. Let’s take some time to learn some more about him.


This week we welcome Tracy Osborn as our PyDev of the Week. She is active in the Django portion of the Python universe. Let’s spend some time getting to know her.



This week we welcome Trey Hunner (@treyhunner) as our PyDev of the Week! Trey writes a programming blog which is worth your time checking out. He is quite active in his local Python community as well as the worldwide community. Let’s spend some time getting to know Trey.


This week we welcome Christopher Clarke (@realchrisdev) as our PyDev of the Week. Christopher writes a Python blog that is worth your time perusing. You might also find his github profile interesting. Let’s spend some time getting to know him better!


This week we welcome Matthew Nuzum as our PyDev of the Week. Matthew has recently completed a Django training video that is being published by Packt Publishing. Let’s spend some time getting to know him better!


I was recently approached by the author of the free eBook, Intermediate Python by Muhammad Yasoob Ullah Khalid to review his work. Yasoob is the fellow behind the Python Tips blog. The book has been released as open source on Github but can be downloaded as a PDF from ReadTheDocs. But before I go into too much detail about the book, here’s my quick review:

Quick Review

  • Why I picked it up: I was asked by the author to read the book.
  • Why I finished it: I read through a lot of the book and skimmed the rest, actually
  • I’d give it to: A beginner who wants to learn a bit more about the Python language


This week we welcome Kenneth Love as our PyDev of the Week. He is active in the Python community via the Portland chapter of Django Ladies. He works for Team Treehouse creating Python courses. Let’s spend some time getting to know him better.


This week we welcome Melanie Crutchfield as our PyDev of the week! Let’s spend some time getting to know her better!



I was recently looking into ways to get my screen resolution with Python to help diagnose an issue with an application that wasn’t behaving correctly. In this article, we’ll look at some of the ways to get your screen resolution. Not all of the solutions will be cross-platform, but I’ll be sure to mention that fact when I discuss those methods. Let’s get started!

Using the Linux Command Line

There are several ways to get your screen resolution in Linux. If you do a Google search, you’ll see people using various Python GUI toolkits. I wanted to find a way to get the screen resolution without installing a third party module. I eventually found the following command:

xrandr | grep '*'

I then had to take that information and translate it into Python. Here’s what I came up with:

import subprocess
cmd = ['xrandr']
cmd2 = ['grep', '*']
p = subprocess.Popen(cmd, stdout=subprocess.PIPE)
p2 = subprocess.Popen(cmd2, stdin=p.stdout, stdout=subprocess.PIPE)
resolution_string, junk = p2.communicate()
resolution = resolution_string.split()[0]
width, height = resolution.split('x')

Whenever you need to pipe data with Python, you will need to create two different subprocess instances. That is what I did above. I piped the output from xrandr to my second subprocess via its stdin. Then I closed the stdout of the first process to basically flush whatever it had returned to the second process. The rest of the code just parses out the width and height of the monitor.


Next Page »