Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/unit-testing/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python“;ImportError:没有名为“的模块”;问题_Python_Unit Testing_Pycharm - Fatal编程技术网

Python“;ImportError:没有名为“的模块”;问题

Python“;ImportError:没有名为“的模块”;问题,python,unit-testing,pycharm,Python,Unit Testing,Pycharm,我正在WindowsXPSP3上运行Python 2.6.1。我的IDE是PyCharm 1.0-Beta 2构建PY-96.1055 我将.py文件存储在名为“src”的目录中;它有一个\uuuuuuu init\uuuuuuuuuuuuuuupy.py文件,除了顶部的“\uuuuuuuuuuuuuuuuuuuu”属性外,该文件为空 其中之一称为Matrix.py: #!/usr/bin/env python """ "Core Python Programming" chapter 6. A

我正在WindowsXPSP3上运行Python 2.6.1。我的IDE是PyCharm 1.0-Beta 2构建PY-96.1055

我将.py文件存储在名为“src”的目录中;它有一个
\uuuuuuu init\uuuuuuuuuuuuuuupy.py
文件,除了顶部的“
\uuuuuuuuuuuuuuuuuuuu
”属性外,该文件为空

其中之一称为Matrix.py:

#!/usr/bin/env python
"""
"Core Python Programming" chapter 6.
A simple Matrix class that allows addition and multiplication
"""
__author__ = 'Michael'
__credits__ = []
__version__ = "1.0"
__maintainer__ = "Michael"
__status__ = "Development"

class Matrix(object):
    """
    exercise 6.16: MxN matrix addition and multiplication
    """
    def __init__(self, rows, cols, values = []):
        self.rows = rows
        self.cols = cols
        self.matrix = values

    def show(self):
        """ display matrix"""
        print '['
        for i in range(0, self.rows):
            print '(',
            for j in range(0, self.cols-1):
                print self.matrix[i][j], ',',
            print self.matrix[i][self.cols-1], ')'
        print ']'

    def get(self, row, col):
        return self.matrix[row][col]

    def set(self, row, col, value):
        self.matrix[row][col] = value

    def rows(self):
        return self.rows

    def cols(self):
        return self.cols

    def add(self, other):
        result = []
        for i in range(0, self.rows):
            row = []
            for j in range(0, self.cols):
                row.append(self.matrix[i][j] + other.get(i, j))
            result.append(row)
        return Matrix(self.rows, self.cols, result)

    def mul(self, other):
        result = []
        for i in range(0, self.rows):
            row = []
            for j in range(0, other.cols):
                sum = 0
                for k in range(0, self.cols):
                    sum += self.matrix[i][k]*other.get(k,j)
                row.append(sum)
            result.append(row)
        return Matrix(self.rows, other.cols, result)

    def __cmp__(self, other):
        """
        deep equals between two matricies
        first check rows, then cols, then values
        """
        if self.rows != other.rows:
            return self.rows.cmp(other.rows)
        if self.cols != other.cols:
            return self.cols.cmp(other.cols)
        for i in range(0, self.rows):
            for j in range(0, self.cols):
                if self.matrix[i][j] != other.get(i,j):
                    return self.matrix[i][j] == (other.get(i,j))
        return True # if you get here, it means size and values are equal



if __name__ == '__main__':
    a = Matrix(3, 3, [[1, 2, 3], [4, 5, 6], [7, 8, 9]])
    b = Matrix(3, 3, [[6, 5, 4], [1, 1, 1], [2, 1, 0]])
    c = Matrix(3, 3, [[2, 0, 0], [0, 2, 0], [0, 0, 2]])
    a.show()
    b.show()
    c.show()
    a.add(b).show()
    a.mul(c).show()
我已经创建了一个名为“test”的新目录,它还有一个
\uuuuu init\uuuuuuuuuuuuuuuuuuuupy
文件,除了顶部的“
\uuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuu作者”属性外,该文件是空的。我已经创建了一个MatrixTest.py来单位我的矩阵类:

#!/usr/bin/env python
"""
Unit test case for Matrix class
See http://jaynes.colorado.edu/PythonGuidelines.html#module_formatting for Python coding guidelines
"""

import unittest #use my unittestfp instead for floating point
from src import Matrix # Matrix class to be tested

__author__ = 'Michael'
__credits__ = []
__license__ = "GPL"
__version__ = "1.0"
__maintainer__ = "Michael"
__status__ = "Development"

class MatrixTest(unittest.TestCase):
    """Unit tests for Matrix class"""
    def setUp(self):
        self.a = Matrix.Matrix(3, 3, [[1, 2, 3], [4, 5, 6], [7, 8, 9]])
        self.b = Matrix.Matrix(3, 3, [[6, 5, 4], [1, 1, 1], [2, 1, 0]])
        self.c = Matrix.Matrix(3, 3, [[2, 0, 0], [0, 2, 0], [0, 0, 2]])

    def testAdd(self):
        expected = Matrix.Matrix(3, 3, [[7, 7, 7], [5, 6, 7], [9, 9, 9]])    # need to learn how to write equals for Matrix
        self.a.add(self.b)
        assert self.a == expected

if __name__ == '__main__':    #run tests if called from command-line
    suite = unittest.TestLoader().loadTestsFromTestCase(TestSequenceFunctions)
    unittest.TextTestRunner(verbosity=2).run(suite)
然而,当我尝试运行MatrixTest时,我得到了以下错误:

C:\Tools\Python-2.6.1\python.exe "C:/Documents and Settings/Michael/My Documents/Projects/Python/learning/core/test/MatrixTest.py"
Traceback (most recent call last):
  File "C:/Documents and Settings/Michael/My Documents/Projects/Python/learning/core/test/MatrixTest.py", line 8, in <module>
    from src import Matrix # Matrix class to be tested
ImportError: No module named src

Process finished with exit code 1
C:\Tools\Python-2.6.1\Python.exe“C:/Documents and Settings/Michael/My Documents/Projects/Python/learning/core/test/MatrixTest.py”
回溯(最近一次呼叫最后一次):
文件“C:/Documents and Settings/Michael/My Documents/Projects/Python/learning/core/test/MatrixTest.py”,第8行,在
从src导入矩阵#待测试矩阵类
ImportError:没有名为src的模块
进程已完成,退出代码为1
我所读到的一切都告诉我,在我的所有目录中使用init.py应该可以解决这个问题

如果有人能指出我错过了什么,我将不胜感激

我还想知道开发和维护源代码和单元测试类的最佳方法。当我编写Java:/src和/test目录时,我通常会这样想,下面有相同的包结构。这是“Pythic”思维,还是我应该考虑另一个组织方案? 更新:

感谢那些回答问题的人,以下是对我有效的解决方案:

  • 将导入从src导入矩阵更改为
    #要测试的矩阵类
  • sys.path
    作为环境变量添加到我的unittest配置中,并使用分号分隔./src和./test目录
  • 如图所示,更改MatrixTest.py中的声明

  • 这有点猜测,但我认为你需要 环境变量,包括src和test目录

    运行
    src
    目录中的程序可能一直在工作,因为Python会自动将当前正在运行的脚本的目录插入到
    sys.path
    中。因此,只要您同时执行驻留在
    src
    中的脚本,在
    src
    中导入模块就可以工作

    但是现在您正在运行来自
    test
    的脚本,
    test
    目录会自动添加到
    sys.path
    ,而
    src
    则不会

    PYTHONPATH中列出的所有目录都被添加到
    sys.path
    ,Python搜索
    sys.path
    以查找模块

    还有,如果你说

    from src import Matrix
    
    然后
    Matrix
    将引用该包,您需要说
    Matrix.Matrix
    来访问该类。

    关于最佳实践,使用与主要源代码相同级别的
    tests
    目录。另一方面,项目喜欢或使用主源代码下的
    test
    子目录


    问题的另一半已经由回答。

    您说您的文件存储在src文件夹中?C:\Tools\Python-2.6.1\Python.exe“C:/Documents and Settings/Michael/My Documents/Projects/Python/learning/core/test/MatrixTest.py”使用
    Python-m test.MatrixTest
    。见@Cristian:I put
    import sys;在名为test.py的文件中打印(sys.path)
    。然后我运行了
    cd/some/other/dir;python/path/to/test.py
    。列出的第一个路径是
    /path/to
    ,而不是
    /some/other/dir
    。因此它显示的是脚本的目录,而不是添加到
    sys.path
    @~unutbu的CWD:你说得对!我发表评论的原因是:“导入名为spam的模块时,解释器将在当前目录中搜索名为spam.py的文件,然后在环境变量PYTHONPATH指定的目录列表中搜索。”。我想应该有人用不那么模棱两可的方式来重新表述它。@Cristian:嗯,这确实令人困惑。(不过,它试图在第二段中加以澄清)。这里还有一个指向文档的链接;我觉得这个更清楚:谢谢你,~unutbu。您和Christian Ciupitu引导我找到的解决方案被添加到我的问题中。@~unutbu:是的,第二段澄清了一些事情,尽管我很久以前阅读教程时,它并不存在,或者我不知何故跳过了它。无论如何,我向文档团队报告了这一点,也许他们会重新表述这一部分,使之更清楚。另外,感谢您提供的
    sys.path
    链接!嗨,克里斯蒂安,谢谢你的例子。我更喜欢您这样做:/src和/test处于同一级别。