0

我正在用鼻子编写一个基本测试,以从日志包装器中调用单个函数,但是一旦我发现了测试,我就开始收到标准库模块导入错误。

这是我正在尝试为其编写一些测试的代码,并且已经投入生产并且很长时间没有改变,所以我确信当我尝试使用鼻子时有问题。

  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

4

1 回答 1

1

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

于 2012-09-13T23:52:56.137 回答