模块*没有使用nosetests的属性*



我从"Python苦学;书它是关于使用nose的测试。我在lexicon.py文件中有函数scan_net

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 testslexicon_tests.py命令后AttributeError:module 'lexicon' has no attribute 'scan_net'

我的进口货出了什么问题?为什么它看不到函数scan_net

您的路径中可能有一个名为lexicon的文件夹,该文件夹是首选文件夹,应该重命名其中一个,以使其更清楚地显示应该导入的文件夹。

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

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

最新更新