我試圖導入一個模塊並在當前的python文件中使用該模塊中的函數。如何從另一個模塊使用python函數
我上運行parser_tests.py文件nosetests但它失敗
例如它沒有找到這在parsrer.py文件中明確規定了parse_subject功能「沒有定義名稱‘parse_subject’」
這是parsrer文件:
def peek(word_list):
if word_list:
word = word_list[0]
return word[0]
else:
return None
#Confirms that the expected word is the right type,
DEF匹配(WORD_LIST,期待): 如果WORD_LIST: 字= word_list.pop(0)
if word[0] == expecting:
return word
else:
return None
else:
return None
DEF跳過(WORD_LIST,word_type): 而PEEK(WORD_LIST)== word_type: 匹配(WORD_LIST,word_type)
DEF parse_verb(WORD_LIST): 跳過(WORD_LIST, '停止')
if peek(word_list) == 'verb':
return match(word_list, 'verb')
else:
raise ParserError("Expected a verb next.")
DEF parse_object(WORD_LIST): 跳過(WORD_LIST, '停止') next_word = PEEK(WORD_LIST)
if next_word == 'noun':
return match(word_list, 'noun')
elif next_word == 'direction':
return match(word_list, 'direction')
else:
raise ParserError("Expected a noun or direction next.")
DEF parse_subject(WORD_LIST): 跳過(WORD_LIST, '停止') next_word = PEEK(WORD_LIST)
if next_word == 'noun':
return match(word_list, 'noun')
elif next_word == 'verb':
return ('noun', 'player')
else:
raise ParserError("Expected a verb next.")
DEF parse_sentence(WORD_LIST): SUBJ = parse_subject(WORD_LIST) 動詞= parse_verb (WORD_LIST) OBJ = parse_object(WORD_LIST)
return Sentence(subj, verb, obj)
這是我的測試文件
from nose.tools import *
從nose.tools導入assert_equals 進口SYS sys.path.append( 「H:/項目/ projectx48/ex48」)
進口parsrer
DEF test_subject(): WORD_LIST = [('noun','bear'),('verb','eat'),('stop','the'),('noun','honey')] assert_equals(parse_subject(word_list),( '名詞','熊'))
你可以發佈如何進口嗎? – levi