Skip to main content


Showing posts from July, 2012

Python: Paths, packages and directory organization

Adding a path (/a) to a .pth file placed in a directory already on python's search path will add /a to the search path.

If we have a subdirectory b under a (/a/b) modules in thsi subdirectory will not be included in the path. A second line /a/b needs to be included in the .pth file

If we add a file to b/ (the file can be empty) then python treats the structure as part of a package and we can then do (assuming is a module in /a/b)

import b.mod

Logging in python

It's just a wee bit tricky to set up python logging such that each module has its own logger and the logger level is controlled by a main script. The way to do that is illustrated below. The crucial line is


in the main script. This line sets up all the loggers from root onwards and sets them to a uniform level.

import logging import module as m logging.basicConfig(level=logging.DEBUG) logger = logging.getLogger(__name__) logger.warning('This is a warning from the main script') m.f1()
import logging logger = logging.getLogger(__name__) def f1():'A warning from the module')