【问题标题】:PYTHON: nosetests import file path with multiple modules/filesPYTHON:nosetests 导入具有多个模块/文件的文件路径
【发布时间】:2016-03-30 19:52:32
【问题描述】:

我目前正在通过LearnPythonTheHardWay 工作,并已联系Exercise 48,其中详细说明了鼻子测试。只要所有代码都在一个 python.py 文件中,我就可以执行单元测试。但是,如果我将其他文件作为程序的一部分包含在内,即使用 import 然后尝试 nosetest 这样的项目,我会收到错误消息,如下所示:

================================================ ========================

错误:失败:ImportError(没有名为“temp”的模块)

回溯(最近一次通话最后一次):
文件“/usr/local/lib/python3.4/dist-packages/nose/failure.py”,第 39 行,在 runTest
提高 self.exc_val.with_traceback(self.tb)
文件“/usr/local/lib/python3.4/dist-packages/nose/loader.py”,第 414 行,在 loadTestsFromName ## ##
addr.filename, addr.module)
文件“/usr/local/lib/python3.4/dist-packages/nose/importer.py”,第 47 行,在 importFromPath
return self.importFromDir(dir_path, fqname)
文件“/usr/local/lib/python3.4/dist-packages/nose/importer.py”,第 94 行,在 importFromDir
mod = load_module(part_fqname, fh, 文件名, desc)
文件“/usr/lib/python3.4/imp.py”,第 235 行,在 load_module
返回加载源(名称,文件名,文件)
文件“/usr/lib/python3.4/imp.py”,第 171 行,在 load_source
模块 = 方法.load()
文件“”,第 1220 行,加载中
文件“”,第 1200 行,在 _load_unlocked
文件“”,第 1129 行,在 _exec
文件“”,第 1471 行,在 exec_module
文件“”,第 321 行,在 _call_with_frames_removed
文件“/home/user/LEARNPYTHONTHEHARDWAY/ex48/tests/scanner_tests.py”,第 6 行,在
从 ex48.scanner 导入词典

中的文件“/home/user/LEARNPYTHONTHEHARDWAY/ex48/ex48/scanner.py”,第 6 行 进口温度
ImportError:没有名为“temp”的模块


在 0.028 秒内运行 1 次测试

失败(错误=1)

我的项目目录结构如下:

ex48/
  ex48/
     scanner.py
     temp.py
  __pycache__/
  tests/
     __init__.py
    scanner_tests.py

我的目录截图::

文件本身的屏幕截图::

我的 scanner_tests.py 文件如下:

from nose.tools import *
from ex48.scanner import lexicon
from ex48 import temp

def test_directions():
    assert_equal(lexicon.scan("north"),[('direction','north')])
        result = lexicon.scan("north south east")
        assert_equal(result, [('direction', 'north'),
                          ('direction', 'south'),
                          ('direction', 'east')])

我的scanner.py文件如下:

   import temp

   class lexicon:
       def scan(val):
          if(val == "north"):
              return [('direction', 'north')]
          else:
              return [('direction', 'north'),
                      ('direction', 'south'),
                      ('direction', 'east')]

    runner = temp.temp("hello")

最后我的temp.py文件如下:

   class temp(object):
       def __init__(self,name):
           self.name = name
       def run(self):
           print "Your name is; %s" % self.name     
    runner.run()

我的问题是如何克服 ImportError: No Module named 'temp' 因为好像我在两个扫描仪中都导入了 temp.py 文件。 py 文件和 scanner_tests.py 文件,但鼻子在运行时似乎无法导入它。 Nosetests 在它只是单个 scanner.py 文件时工作正常,但在导入时却不行。是否有特殊的语法可以导入到鼻子的单元测试中?该脚本在命令行中正确运行和导入时也可以正常工作。

*注意:我正在从在线服务器的受限帐户中运行 python,因此某些管理员权限不可用。

**注意以下是来自另一个项目的完全不同的屏幕截图,具有完全相同的错误:

目录布局:

游戏.py:

Otherpy.py - 导入的文件:

鼻子测试脚本文件:

最后是鼻子测试导入错误:

【问题讨论】:

  • 查看您的文件布局,您在设置和 temp.py 所在的 ex48 文件夹中缺少 __init__.py。你能确保你有适当的__init__.py,然后再试一次吗?
  • 感谢--但仍然得到相同的结果:/
  • 你从哪里流鼻涕?在ex48 目录内?
  • 是的,最上面的 ex48 目录是 ex48/ 和 tests/
  • 您的 __init__.py 文件到底在哪里?

标签: python unit-testing import nose


【解决方案1】:

一切都需要与您的执行点相关。您正在从 ex48 的根目录运行您的 nose 命令,因此您的所有导入都需要相对于该位置。

因此,在game.py 中,您应该相对于ex48 进行导入。因此:

from ex48.otherpy import House

应将相同的逻辑应用于引用 temp 文件夹的示例。

from ex48.temp import temp

【讨论】:

    【解决方案2】:

    我找到的唯一解决方案是将以下内容发布到主文件的顶部:

    try: 
         # This handles imports when running .py files from inside app directory
         from file_to_import.py import class_instance
    except:
         # This handles imports when running nosetests from top-level (above app) 
         # directory
         from directory_containing_app_files.file_to_import import class_instance
    

    我对替代解决方案非常感兴趣。

    【讨论】:

      猜你喜欢
      • 2019-01-27
      • 2023-04-03
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2022-01-09
      • 2014-06-15
      • 2010-10-24
      相关资源
      最近更新 更多