[英]Importing modules into python shell
我试图直接从python shell测试我的文件,而不是在我的.py文件中运行它。 但是,每当我导入模块并调用函数时,它都会显示NameError:即使已定义名称“ evaluate_essay”。 我该如何解决?
这是程序的代码:
def evaluate_essay(essayFilename):
fileList= []
file= open(essayFilename, "r")
fileList= [file.read().split()]
file.close()
longWords=0
medWords=0
shortWords=0
#nested for loop that checks every word in list
for i in range (len(fileList)):
for k in range (len(fileList[0])):
if (len(fileList[0][k])) >= 7:
longWords += 1
if 4<=(len(fileList[0][k]))<=6:
medWords += 1
if (len(fileList[0][k])) <= 3:
shortWords += 1
#if statements that determines level of each essay
if (longWords) >= (len(fileList[0])/2):
print ("This is a COLLEGE LEVEL essay")
elif (longWords)>(medWords) and (longWords)>(shortWords):
print ("This is a HIGH SCHOOL LEVEL essay")
elif (medWords)>(longWords) and (medWords)>(shortWords):
print ("This is a MIDDLE SCHOOL LEVEL essay")
else:
print ("This is an ELEMENTARY SCHOOL LEVEL essay")
evaluate_essay()
首先,去掉调用evaluate_essay
从脚本,或者给它一个说法。 您当前在不带参数的情况下调用它,但是它需要一个参数。
要从交互式会话中调用此功能,首先需要导入模块。 必须满足以下条件之一。
PYTHONPATH
中的目录中。 现在,在解释器中,有两种方法。 一种是导入模块,并在其属性的名称之前添加对引用的前缀,就像这样。
import problem3
problem3.evaluate_essay(my_file_name)
另一种方法是显式导入函数,并使用不合格的名称。
from problem3 import evaluate_essay
evaluate_essay(my_file_name)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.