Skip to main content

Posts

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 __init__.py to b/ (the file can be empty) then python treats the structure as part of a package and we can then do (assuming mod.py 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

logging.basicConfig(level=logging.DEBUG)

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

main.py

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()

module.py
import logging logger = logging.getLogger(__name__) def f1(): logger.info('A warning from the module')