Pylint说模块无法导入

时间:2018-05-22 23:23:01

标签: python unit-testing pylint

我有一个Python模块,可以对我编写的模块执行单元测试。单元测试放在名为tests的文件夹中,我测试的模块是上面的一个目录。所以,我的单元测试文件有以下标题。

import unittest
import sys
sys.path.append('../')
import table_builder

但是当我提到这个单元测试代码时,我收到以下错误:

No config file found, using default configuration
************* Module unittests_table_builder
E:  9, 0: Unable to import 'table_builder' (import-error)
C:  9, 0: Import "import table_builder" should be placed at the top of the           module (wrong-import-position)

------------------------------------------------------------------
Your code has been rated at 4.55/10 (previous run: 4.55/10, +0.00)

我该如何解决这个问题?

1 个答案:

答案 0 :(得分:0)

如果从命令行调用测试程序,它将自动为您更新sys.path,而无需在测试中执行此操作。使用test discovery模式,它将在目录树中找到并运行所有测试。也可以进行单独的测试。

$ python -m unittest discover -s project_directory -p "*_test.py"

您还可以使用relative import语法从父模块导入:

from .. import my_module
相关问题