1

以下代码由 PyDev eclipse 插件自动生成,通过选择“new --> PyDev project”,新建“PyDev Module”,然后选择“Unittest”

但在eclipse编辑器上,“import unittest”上显示一个红色标记,表示模块无法导入。但这件事运行良好。我正在使用 python 2.7 和 eclipse 3.6

'''
Created on Dec 12, 2012

@author: yang
'''
import unittest


class Test(unittest.TestCase):


    def testName(self):
        pass


if __name__ == "__main__":
    #import sys;sys.argv = ['', 'Test.testName']
    unittest.main()
4

3 回答 3

1

解决方案如下: -

我通过以下方式删除了这些错误:

Window -> Preferences -> PyDev -> Editor -> Code Analysis -> Undefined -> Undefined Variable From Import -> Ignore

对于导入未找到错误:-

也可能是,

Window -> Preferences -> PyDev -> Editor -> Code Analysis -> Imports -> Import not found -> Ignore

我们强制删除这些错误,因为 python 解释器对此没有解决方案。

如果您需要这方面的更多信息,请告诉我。

于 2013-10-25T08:15:10.573 回答
1

您必须在本地系统中有一个 unittest.py 文件,该文件与 unittest 模块混淆。重命名您的文件并连同此一起删除 .pyc 文件。

于 2016-01-05T12:13:50.583 回答
0

I can think of 2 reasons for this to fail:

  1. The interpreter you have configured doesn't have the .py files, only .pyc files (for PyDev to work properly, you need to have an interpreter with the actual .py files).

  2. The interpreter is not properly configured (i.e.: you created some virtualenv which references a parent env but the paths from the parent env were not properly added in the interpreter configuration) -- i.e.: the /Lib which has the unittest package (with unittest/__init__.py) is not added to the PYTHONPATH at preferences > pydev > interpreters > python interpreter > select interpreter > libraries).

于 2016-01-07T10:48:29.273 回答