首页 > 解决方案 > 模块 * 没有属性 * 使用鼻子测试

问题描述

我从“Python Learn Hard Way”一书中完成了任务。它是关于使用nose. 我scan_netlexicon.py文件中有函数:

def scan_net(sentence):
    direction = ['north', 'south', 'east', 'west']
    verb = ['go', 'kill', 'eat', 'breath']
    stop_words = ['the', 'in', 'off']
    nouns = ['bear', 'princess', 'frog']

    words = sentence.split()
    result = []
    for i in range(len(words)):
        if words[i] in direction:
            result.append(('direction', words[i]))
        elif words[i] in verb:
            result.append(('verb', words[i]))
        elif words[i] in stop_words:
            result.append(('stop_words', words[i]))
        elif words[i] in nouns:
            result.append(('nouns', words[i]))
        #check for number and if it is go out of the loop using continue
        try:
            if(int(words[i])):
                result.append(('number', words[i]))
                continue
        except ValueError:
            pass
        else:
            result.append(('error', words[i]))
    return result

这是我的测试文件:

from nose.tools import *
import lexicon

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

运行nosetests tests\lexicon_tests.py命令后,我得到 AttributeError:module 'lexicon' has no attribute 'scan_net'.

我的导入有什么问题?为什么看不到功能scan_net

标签: pythonattributeerrornose

解决方案


您可能有一个lexicon在您的路径中命名的文件夹是首选的,应该重命名一个或另一个,以便更清楚应该导入哪个文件夹。

您应该仍然可以使用 导入from lexicon import scan_net,但通常使用不同的名称会使您的生活更轻松。

请参阅与目录同名的 Python 导入类


推荐阅读