Now I’m attempting bigger programs, I’d like to use the logging module rather than peppering my code with prints. But I’ve fallen at the first. I have a two page program, added the simplest logging stuff I could, and it didn’t work. So, I ran the example, and it worked. I then dribbled my code into the example line by line, until I found what stopped it. A single trivial import of a near-empty module stops the proper logging behaviour.
# import ntu.dummy
import logging
LOG_FILENAME = 'example_15.log'
print 'before basicconfig'
logging.basicConfig(filename=LOG_FILENAME,level=logging.DEBUG)
print 'before log write'
logging.debug('This message should go to the log file')
print 'after log write'
With # import ntu.dummy, the program runs, prints out the debugs, and deposits the expected .log file, with the expected contents, into the program’s folder. This is true from within IDLE, or directly in the OS.
If I remove the # to allow import ntu.dummy to execute, then the program runs, prints my debugs, but no .log file is created, neither in the program’s folder, nor AFAICS in any other location on the machine.
C:\Python26\Lib\ntu\ contains an __init__.py, and this file dummy.py, which has contents …
def bolleaux():
""" empty function """
return None
In place of the import ntu.dummy statement, I can have all sorts of other imports, random, Tkinter, os.path, which don’t cause the logging to fail
Help, what’s going on please?
Doh!
The ntu package contains many other single function or single class modules, really for ease of testing and updating. Some of these have come from a friend, so I wasn’t familiar with what was in them. As the width of the package was getting unwieldy, He suggested that in __init__.py I put …
for each module I have in there, which then brings ClassName into the ntu namespace. This means that when I want to use any of the modules, I just use
And of course, yes, one of his modules does a logging.basicConfig, at the module level before the class definition. So, even though I only import my module, doing so runs __init__, which imports his module, which snafus subsequent use of logging. Commenting out any link in that chain makes logging work properly.
I’m not sure that using import in the package is such a huge saving, and it lays me open to nasty side effects like this. So I’m going back to importing from packname.modname for all my modules.
But I will want to use that dodgy module at some time, so will have to make it safer, or get him to make it safer.
Many thanks