python - 模块 * 没有属性 * 使用鼻子测试
问题描述
我从“Python Learn Hard Way”一书中完成了任务。它是关于使用nose
. 我scan_net
在lexicon.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
?
解决方案
您可能有一个lexicon
在您的路径中命名的文件夹是首选的,应该重命名一个或另一个,以便更清楚应该导入哪个文件夹。
您应该仍然可以使用 导入from lexicon import scan_net
,但通常使用不同的名称会使您的生活更轻松。
推荐阅读
- firebase - 身份验证选项 (Firebase) 的优点/缺点
- javascript - 如果年份大于当前年份,是否可以替换文本?
- android - ExoPlayer 与 Jetpack Compose - 方向更改重新启动视频
- makefile - gnu 制作模式以匹配目录
- excel - 使用具有多个值的列表中的标题和描述列创建表
- sql - 如何在 postgresql 中删除现有索引
- c++ - C++ 命令行中的 CD 命令
- typescript - 右侧为“未定义”的空值合并运算符 (??)
- python-3.x - Python 日期时间时区偏移
- r - Ggplot2 上的双 y 轴