Interesting Python Standard Library Modules

Python developers use pypi to find packages. You need to search, filter some packages, read reviews from other developers, read the documentation and explore examples if exists. Anyone can build and deploy a package to pypi so you really don’t know if the package you found is working ok.

Python standard library is full of useful modules so before looking outside, it is important to be familiar with it.

Pretty Printer – pprint

If you program print information or you are using the print function for debugging this module will be very helpful. For example, if you have the following dictionary:

the output looks like:

One long line and not so easy to read. With pprint module:

the output:

It can be used on any python structure, in this case, the dictionary is sorted by key. You can controlĀ indentation, width, depth and more using the PrettyPrinter object. See the docs for details


Working with temporary files – tempfile

While working with data, sometimes we need to create a temporary file. The module tempfile helps to create temp files and directories based on the running OS for example:

the result is a tuple with the file descriptor as the first element (or other OS file handle) and the full path as the second

the next module is used to access the file


Operating System Specific – os

os module is very useful, it contains many os specific routines for working with processes, files, IPC and other OS objects.

If for example, you want to use the tempfile generated file descriptor for read and write you can use os module:


Tiny shell with shutil

If you need to manage files – copy, delete, move and more, this module offers some high-level operations that can help

For example to copy a folder recursively

To delete a folder recursively :

You can use this module with help of os module to create a tiny shell using python


Working with signals

Signals is a good way for communication between processes. One application can register a signal handler and the other can send it. While a signal received in the process, one of its threads will stop and handle that signal. You can also use signals to output current status on a long processing operation.

For example:

In this example, the script registers a signal handler for CTRL+C, then it runs an infinite loop and increments a counter every second. To print the current counter value, press CTRL+C

You can register a user define signal using SIGUSR1, SIGUSR2

to send a signal from other process use os.kill

The best way to create this is to create parent-child processes using fork. See details here


Handling date and time

Date and time manipulation is a common task in many programs. There are three related modules in the Standard Library which help

  • datetime
  • calendar
  • time

datetime module includes datetime objects for extracting date and time in a different format, timedelta for diff calculations, time zones, formatting and more

calendar module includes some useful iterators

For example, iterate over all days in Feb 2016:

time moduleĀ supports mktime(), localtime(), gmtime(), strftime(), sleep() and more


The platform module

used to identify the platform we are using:



Network Programming with socket module

The socket module is a network programming interface. The API also contains a set of Python-specific extensions.

Code written using this module is portable. You can communicate easily between python and other languages using socket. For secure connection use ssl module

Server example:

Client side:


There are many more important modules in Python standard library – see the docs










Leave a Reply

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