Log in | Back to darenet.org

Python en:Standard Library

In This Guide:

Introduction

The Python Standard Library contains a huge number of useful modules and is part of every standard Python installation. It is important to become familiar with the Python Standard Library since many problems can be solved quickly if you are familiar with the range of things that these libraries can do.

We will explore some of the commonly used modules in this library. You can find complete details for all of the modules in the Python Standard Library in the 'Library Reference' section of the documentation that comes with your Python installation.

Let us explore a few useful modules.

Note
If you find the topics in this chapter too advanced, you may skip this chapter. However, I highly recommend coming back to this chapter when you are more comfortable with programming using Python.

sys module

The sys module contains system-specific functionality. We have already seen that the sys.argv list contains the command-line arguments.

Suppose we want to check the version of the Python command being used so that, say, we want to ensure that we are using at least version 3. The sys module gives us such functionality.

<source lang="python"> >>> import sys >>> sys.version_info (3, 0, 0, 'beta', 2) >>> sys.version_info[0] >= 3 True </source>

How It Works:

The sys module has a version_info tuple that gives us the version information. The first entry is the major version. We can check this to, for example, ensure the program runs only under Python 3.0:

<source lang="python">

  1. !/usr/bin/python
  2. Filename: versioncheck.py

import sys, warnings if sys.version_info[0] < 3:

   warnings.warn("Need Python 3.0 for this program to run",
       RuntimeWarning)

else:

   print('Proceed as normal')

</source>

Output:

   $ python2.5 versioncheck.py
   versioncheck.py:6: RuntimeWarning: Need Python 3.0 for this program to run
     RuntimeWarning)
   
   $ python3 versioncheck.py
   Proceed as normal

How It Works:

We use another module from the standard library called warnings that is used to display warnings to the end-user. If the Python version number is not at least 3, we display a corresponding warning.

logging module

What if you wanted to have some debugging messages or important messages to be stored somewhere so that you can check whether your program has been running as you would expect it? How do you "store somewhere" these messages? This can be achieved using the logging module.

<source lang="python">

  1. !/usr/bin/python
  2. Filename: use_logging.py

import os, platform, logging

if platform.platform().startswith('Windows'):

   logging_file = os.path.join(os.getenv('HOMEDRIVE'), os.getenv('HOMEPATH'), 'test.log')

else:

   logging_file = os.path.join(os.getenv('HOME'), 'test.log')

logging.basicConfig(

   level=logging.DEBUG,
   format='%(asctime)s : %(levelname)s : %(message)s',
   filename = logging_file,
   filemode = 'w',

)

logging.debug("Start of the program") logging.info("Doing something") logging.warning("Dying now") </source>

Output:

   $python use_logging.py
   Logging to C:\Users\swaroop\test.log

If we check the contents of test.log, it will look something like this:

   2008-09-03 13:18:16,233 : DEBUG : Start of the program
   2008-09-03 13:18:16,233 : INFO : Doing something
   2008-09-03 13:18:16,233 : WARNING : Dying now

How It Works:

We use three modules from the standard library - the os module for interacting with the operating system, the platform module for information about the platform i.e. the operating system and the logging module to log information.

First, we check which operating system we are using by checking the string returned by platform.platform() (for more information, see import platform; help(platform)). If it is Windows, we figure out the home drive, the home folder and the filename where we want to store the information. Putting these three parts together, we get the full location of the file. For other platforms, we need to know just the home folder of the user and we get the full location of the file.

We use the os.path.join() function to put these three parts of the location together. The reason to use a special function rather than just adding the strings together is because this function will ensure the full location matches the format expected by the operating system.

We configure the logging module to write all the messages in a particular format to the file we have specified.

Finally, we can put messages that are either meant for debugging, information, warning or even critical messages. Once the program has run, we can check this file and we will know what happened in the program, even though no information was displayed to the user running the program.

urllib and json modules

How much fun would it be if we could write our own program that will get search results from the web? Let us explore that now.

This can be achieved using a few modules. First is the urllib module that we can use to fetch any webpage from the internet. We will make use of Yahoo! Search to get the search results and luckily they can give us the results in a format called JSON which is easy for us to parse because of the built-in json module in the standard library.

TODO
This program doesn't work yet which seems to be a bug in Python 3.0 beta 2.

<source lang="python">

  1. !/usr/bin/python
  2. Filename: yahoo_search.py

import sys if sys.version_info[0] != 3:

   sys.exit('This program needs Python 3.0')

import json import urllib, urllib.parse, urllib.request, urllib.response

  1. Get your own APP ID at http://developer.yahoo.com/wsregapp/

YAHOO_APP_ID = 'jl22psvV34HELWhdfUJbfDQzlJ2B57KFS_qs4I8D0Wz5U5_yCI1Awv8.lBSfPhwr' SEARCH_BASE = 'http://search.yahooapis.com/WebSearchService/V1/webSearch'

class YahooSearchError(Exception):

   pass
  1. Taken from http://developer.yahoo.com/python/python-json.html

def search(query, results=20, start=1, **kwargs):

   kwargs.update({
       'appid': YAHOO_APP_ID,
       'query': query,
       'results': results,
       'start': start,
       'output': 'json'
   })
   url = SEARCH_BASE + '?' + urllib.parse.urlencode(kwargs)
   result = json.load(urllib.request.urlopen(url))
   if 'Error' in result:
       raise YahooSearchError(result['Error'])
   return result['ResultSet']

query = input('What do you want to search for? ') for result in search(query)['Result']:

   print("{0} : {1}".format(result['Title'], result['Url']))

</source>

Output:

TODO

How It Works:

We can get the search results from a particular website by giving the text we are searching for in a particular format. We have to specify many options which we combine using key1=value1&key2=value2 format which is handled by the urllib.parse.urlencode() function.

So for example, open this link in your web browser and you will see 20 results, starting from the first result, for the words "byte of python", and we are asking for the output in JSON format.

We make a connection to this URL using the urllib.request.urlopen() function and pass that file handle to json.load() which will read the content and simultaneously convert it to a Python object. We then loop through these results and display it to the end-user.

Module of the Week Series

There is much more to be explored in the standard library such as debugging, handling command line options, regular expressions and so on.

The best way to further explore the standard library is to read Doug Hellmann's excellent Python Module of the Week series.

Summary

We have explored some of the functionality of many modules in the Python Standard Library. It is highly recommended to browse through the Python Standard Library documentation to get an idea of all the modules that are available.

Next, we will cover various aspects of Python that will make our tour of Python more complete.