【问题标题】:python - unittest - ImportError: Start directory is not importablepython - unittest - ImportError:开始目录不可导入
【发布时间】:2018-01-19 18:18:30
【问题描述】:

我正在关注 python unittest 进行一些测试并使用发现功能将测试打包到套件中。 但是,当我尝试使用 unittest 运行测试时,出现此错误:

Traceback (most recent call last):
   File "D:/Project/run_tests.py", line 12, in <module>
     suite2 = unittest.defaultTestLoader.discover(dir2, pattern='test*.py')
   File "C:\Python\Python36-32\lib\unittest\loader.py", line 338, in discover
     raise ImportError('Start directory is not importable: %r' % start_dir)
ImportError: Start directory is not importable: 'D:\\Project\\dir2'

这就是 run_tests.py 的样子:

import unittest

if __name__ == "__main__":

    dir1 = "./test1"
    suite1 = unittest.defaultTestLoader.discover(dir1, pattern='test*.py')
    runner1 = unittest.TextTestRunner()
    runner1.run(suite1)


    dir2 = "./tes2"
    suite2 = unittest.defaultTestLoader.discover(dir2, pattern='test*.py')
    runner2 = unittest.TextTestRunner()
    runner2.run(suite2)

【问题讨论】:

  • 我在错误的目录中遇到了这个问题。我试图访问一些东西,就好像我在顶级目录中一样,结果发现我在其中一个子文件夹中;P

标签: python python-unittest


【解决方案1】:

有一个非常相似的错误,但不涉及符号链接。

这是我的目录布局。

- project root
  - foo
  - bar
  - test

foo 和 bar 被认为是顶级包,其中 test 是所有的测试用例。

在intellij中运行unittest时,我使用了以下参数:

  • 目标:脚本路径设置为test 文件夹
  • 工作目录是项目根目录。

这个设置给了我Start directory is not importable 错误。

我在 unittest.loader.py 中追踪了源代码,发现它会检查测试文件夹中的 __init__.py。所以解决方案是在我的test 文件夹中添加一个__init__.py。希望这对某人有所帮助。

-- 编辑--

我在 python 3.6,mac osx 上

【讨论】:

  • _init__.py 文件的添加解决了我的问题,谢谢
  • 是的,提示看看代码做了什么(例如,寻找__init__.py非常好)。不过,我想知道为什么要这样做,因为我们也可以拥有没有 init.py 的命名空间包
  • 仅供参考:使用 Django 3.0.3 和 Python 3.8.2,将 __init__.py 添加到我的 tests/ 文件夹并删除 tests.py 仍然会有所不同。感谢您向 cmets / answers 添加版本。
【解决方案2】:

我在 CI Pipeline 上测试时遇到了非常相似的问题。就我而言,有一个间接级别文件夹/topleveldirectory/tests

所以我在我的 unittest 命令中添加了 -s:

python -m unittest discover -s folder/topleveldirectory/tests

【讨论】:

  • 我一开始有这个,但它需要在测试代码中添加一些额外的代码才能将项目代码包含到搜索路径中。指定-t 也解决了这个问题,但只有当我添加__init__.py 时才有效,正如Douglas Liu 在他的回答中提到的那样。
  • 感谢您的提示。它表明我的测试和检测确实有效。
【解决方案3】:

有一个similar question with a helpful answer here

但是,如果您在 Linux 中使用像 PyCharm 这样的 IDE 并在 soft-linked 目录中打开文件,就会发生这种情况。运行测试的编辑器似乎对同一模块的两条路径感到困惑,并说其中一条不存在。将所有内容放在一个位置而不使用任何软链接为我解决了这个问题。

【讨论】:

    【解决方案4】:

    在我使用 PyCharm/IntelliJ 的 python 项目中,我经常会遇到这个问题。所以我需要结合上面的两个提示,以便让它在我的平台(通常是 Linux)上运行。所以我通过以下步骤启动并运行它:

    1. 在我的单元测试目录中创建一个空的__init__.py
    2. 请参阅符号链接目录的来源,以便 PyCharm 仅在本机目录上工作。
    3. 在 PyCharm 中将单元测试目录标记为 Test Sources Root
    4. 将单元测试使用的引用 Python 代码标记为 Sources Root

    当引用符号链接目录时,尝试运行单元测试时发生以下错误:

    回溯(最近一次通话最后): 文件“/home/z003yb2k/.IntelliJIdea2019.2/config/plugins/python/helpers/pycharm/_jb_unittest_runner.py”,第 35 行,在 主要(argv=args,模块=无,testRunner=unittestpy.TeamcityTestRunner,缓冲区=不是 JB_DISABLE_BUFFERING) 文件“/usr/lib/python3.7/unittest/main.py”,第 100 行,在 __init__ self.parseArgs(argv) 文件“/usr/lib/python3.7/unittest/main.py”,第 124 行,在 parseArgs self._do_discovery(argv[2:]) _do_discovery 中的文件“/usr/lib/python3.7/unittest/main.py”,第 244 行 self.createTests(from_discovery=True, Loader=Loader) 文件“/usr/lib/python3.7/unittest/main.py”,第 154 行,在 createTests self.test = loader.discover(self.start, self.pattern, self.top) 文件“/usr/lib/python3.7/unittest/loader.py”,第 349 行,在发现中 测试=列表(self._find_tests(start_dir,模式)) _find_tests 中的文件“/usr/lib/python3.7/unittest/loader.py”,第 387 行 名称 = self._get_name_from_path(start_dir) _get_name_from_path 中的文件“/usr/lib/python3.7/unittest/loader.py”,第 371 行 assert not _relpath.startswith('..'), "路径必须在项目内" AssertionError:路径必须在项目内

    【讨论】:

      【解决方案5】:

      似乎许多不同的问题都会产生此错误消息。如果使用 PyCharm,请务必检查 IDE 的右上角是否有任何错误标记,并首先解决它们。
      我的项目目录结构是:

      root
      |- src
      |-- __init__.py  # empty
      |-- py1.py
      |- tests
      |-- test_py1.py
      

      然后我从根目录成功运行了以下命令:

      $ python -m unittest discover -s tests
      

      出于好奇,我还运行了以下更具描述性的命令:

      $ python -m unittest discover -s tests -t src
      

      希望它也能正常工作,但它因错误而失败

      ImportError: Start directory is not importable: '...\\root\\tests'
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2015-01-11
        • 1970-01-01
        • 2013-11-10
        • 2016-03-27
        • 1970-01-01
        • 1970-01-01
        • 2020-01-23
        相关资源
        最近更新 更多