Listing stopped importing trivial function?

Now I am trying to use larger programs, I would like to use the logging module rather than print my code using fingerprints. But I fell for the first. I have a two page program added the simplest post I could and it didn't work. So I ran the example and it worked. Then I translated my code into an example line by line until I found what stopped it. A single trivial import of a nearly empty module stops the correct logging behavior.

# 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'

      

C # import ntu.dummy, the program launches, outputs debugs, and puts the expected .log file with the expected contents into the program folder. This is true from IDLE or directly into the OS.

If I remove the # to allow the import of ntu.dummy, then the program launches, prints my debugs, but the .log file is not generated either in the program folder or in AFAICS anywhere else on the machine.

C: \ Python26 \ Lib \ ntu \ contains __init__.py and this dummy.py file which has content ...

def bolleaux():
    """ empty function """
    return None

      

Instead of the import ntu.dummy statement, I can have all sorts of other import, random, Tkinter, os.path that do not fail the registration

Help what happens next?

+3


source to share


1 answer


Doh!

The ntu package contains many other modules with one or more modules, making it really easy to test and upgrade. Some of them came from a friend, so I was not familiar with what was in them. As the width of the package was getting unwieldy, he suggested that in __init__.py I put ...

from modname import ClassName

      

for every module I have, which then injects the ClassName into the ntu namespace. This means that when I want to use any of the modules, I simply use

import ntu       # instead of import ntu.modname for each module !

      



And of course yes, one of its modules does logging.basicConfig at the module level before defining the class. So even though I only import my module, __init__ is done to do this, which imports its module, which then uses log usage. By commenting out any link in this thread, the log works correctly.

I'm not sure if using in-package imports is such a huge savings and it opens up some nasty side effects for me. So I fall back to import from package name .modname for all my modules.

But I will want to use this dodgy module at some point, so it will need to make it more secure or force it to make it more secure.

Many thanks

0


source







All Articles