无法在鼻子测试中导入logging.handlers

时间:2012-09-13 23:15:55

标签: python importerror nose

我正在编写一个带有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

是什么导致这种情况?

更新 我把它简化为以下内容,测试仍然失败,但在鼻子之外,这一切都按预期工作。

from logging.handlers import WatchedFileHandler, TimedRotatingFileHandler

def test_INFO():
    assert 0 == 0

**更新2 ** 我检查过我的路径。在鼻子测试中,我的路径看起来像这样。

['/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']

但我在python解释器中的正常路径是

['', '/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']

我正在运行nosetests-2.4 tests

tests/unit/test_log.py

中有一个文件

1 个答案:

答案 0 :(得分:1)

我的操作系统默认为使用python 2.4的nosetests-2.4,但我们所有的代码都是python 2.7,所以这就是为什么路径错误而且没有任何工作,而logging.handlers只是第一个丢失的包。 2.4需要的库。