Joshua Olson Joshua Olson - 1 month ago 9
Python Question

Can't import logging.handlers inside a nose test

I'm writing a basic test with nose to call a single function from a logging wrapper, but once I got the test to be discovered I started getting standard library module import errors.

This is the code I'm trying to write some tests for and has been in production and hasn't changed in a long time, so I'm sure the there's something wrong when how I'm trying to use nose.

Failure: ImportError (cannot import name WatchedFileHandler) ... ERROR

======================================================================
ERROR: Failure: ImportError (cannot import name WatchedFileHandler)
----------------------------------------------------------------------
Traceback (most recent call last):
File "/opt/local/lib/python2.4/site-packages/nose/loader.py", line 389, in loadTestsFromName
module = self.importer.importFromPath(
File "/opt/local/lib/python2.4/site-packages/nose/importer.py", line 39, in importFromPath
return self.importFromDir(dir_path, fqname)
File "/opt/local/lib/python2.4/site-packages/nose/importer.py", line 86, in importFromDir
mod = load_module(part_fqname, fh, filename, desc)
File "/Users/jolson/project/qworker/tests/unit/test_qscript_log.py", line 2, in ?
from logging.handlers import WatchedFileHandler, TimedRotatingFileHandler
ImportError: cannot import name WatchedFileHandler


What could cause this?

Update:
I've simplified it to the following and the test still fails, but outside of nose, this all works as expected.

from logging.handlers import WatchedFileHandler, TimedRotatingFileHandler

def test_INFO():
assert 0 == 0


** Update 2 **
I've checked my paths. Inside the nosetests, my path looks like this.

['/Users/jolson/project/qworker/tests', '/Users/jolson/project/qworker',
'/opt/local/bin', '/opt/local/lib/python24.zip', '/opt/local/lib/python2.4',
'/opt/local/lib/python2.4/plat-darwin', '/opt/local/lib/python2.4/plat-mac',
'/opt/local/lib/python2.4/plat-mac/lib-scriptpackages', '/opt/local/lib/python2.4/lib-tk',
'/opt/local/lib/python2.4/site-packages/readline', '/opt/local/lib/python2.4/lib-dynload',
'/opt/local/lib/python2.4/site-packages', '/opt/local/lib/python2.4/site-packages/setuptools-0.6c11-py2.4.egg-info']


but my normal path inside the python interpreter is

['', '/usr/local/bin', '/Library/Python/2.7/site-packages/pip-1.2.1-py2.7.egg',
'/Library/Python/2.7/site-packages/virtualenv-1.8.2-py2.7.egg',
'/Library/Python/2.7/site-packages/virtualenvwrapper-3.6-py2.7.egg',
'/Library/Python/2.7/site-packages/stevedore-0.3-py2.7.egg',
'/Library/Python/2.7/site-packages/virtualenv_clone-0.2.4-py2.7.egg',
'/Library/Python/2.7/site-packages/distribute-0.6.28-py2.7.egg',
'/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python27.zip',
'/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7',
'/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/plat-darwin',
'/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/plat-mac',
'/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/plat-mac/lib-scriptpackages',
'/System/Library/Frameworks/Python.framework/Versions/2.7/Extras/lib/python',
'/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/lib-tk',
'/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/lib-old',
'/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/lib-dynload',
'/System/Library/Frameworks/Python.framework/Versions/2.7/Extras/lib/python/PyObjC',
'/Library/Python/2.7/site-packages', '/Library/Python/2.7/site-packages/setuptools-0.6c11-py2.7.egg-info',
'/Library/Python/2.7/site-packages/IPython/extensions']


I'm running
nosetests-2.4 tests


which has one file in
tests/unit/test_log.py

Answer

My os was defaulting to nosetests-2.4 which uses python 2.4, but all our code is python 2.7, so that's why the path was wrong and nothing was working and logging.handlers was just the first package that was missing from 2.4 libs that was needed.

Comments