如何解决导入功能失败,但是导入功能测试有效
我使用myfuncs导入函数,但失败了,我确实测试了相同的文件路径,只是文件名稍有不同,但失败了。 python代码已有5年历史了,不确定是否有所更改。
我确实进入了该线程,并且进行了测试,并且在测试中它确实可以成功运行,并且我的实际外观是相同的。
function is not defined error in Python
#!/usr/bin/python
# Our function is pulled in here
from myfunction import pyth_test
pyth_test(1,2)
此测试有效,但是我的实际功能文件不是两个文件都在同一目录中,两个导入都是从import *相同的格式开始的,只是文件名略有不同而已。
测试是否可以导入
from myfunction import *
pyth_test(1,2)
此方法成功
3
however when I try the actual impor of the function I need to use I get this error
---------------------------------------------------------------------------
NameError Traceback (most recent call last)
<ipython-input-5-1a2412372452> in <module>
6 f.close()
7
----> 8 bag_of_words = myfuncs.get_bag_of_words(titles_lines)
9 keywords = myfuncs.get_keywords(titles_lines,bag_of_words)
NameError: name 'myfuncs' is not defined
代码下面是我运行的从名为myfuncs.py的文件中调用该函数的
run.py
from myfuncs import *
f = open('s2-titles.txt',encoding="utf8")
titles_lines = f.readlines()
f.close()
bag_of_words = myfuncs.get_bag_of_words(titles_lines)
keywords = myfuncs.get_keywords(titles_lines,bag_of_words)
myfuncs.py
#!/usr/bin/env python
# coding: utf-8
def get_bag_of_words(titles_lines):
# bag of words
bag_of_words = {}
# [1: ]skips the first line which is the header
for line in titles_lines[1:]:
courseid,course_bag_of_words = get_course_bag_of_words(line)
for word in course_bag_of_words:
if word not in course_bag_of_words:
bag_of_words[word] = course_bag_of_words[word]
else:
bag_of_words[word] += course_bag_of_words[word]
return bag_of_words
def get_course_bag_of_words(line):
course_bag_of_words = {}
#split by weirdcombo to prevent weird splits
courseid,title,description = line.split('XXXYYYZZZ')
title = title.lower()
description = description.lower()
wordlist = title.split() + description.split()
if len(wordlist) >=10:
for word in wordlist:
if word not in course_bag_of_words:
course_bag_of_words[word] = 1
else:
course_bag_of_words[word] += 1
return courseid,course_bag_of_words
def get_sorted_results(d):
kv_list = d.items()
vk_list = []
for kv in kv_list:
k,v = kv
vk = v,k
vk_list.append(vk)
vk_list.sort()
vk_list.reverse()
k_list = []
for vk in vk_list[:10]:
v,k = vk
k_list.append(k)
return k_list
def get_keywords(titles_lines,bag_of_words):
n = sum(bag_of_words.values())
keywords = {}
for line in titles_lines[1:]:
courseid,course_bag_of_words = get_course_bag_of_words(line)
term_importance = {}
for word in course_bag_of_words:
tf_course =(float(course_bag_of_words[word])/
sum(course_bag_of_words.values())
)
tf_overall = float(bag_of_words[word]) /n
term_importance[word] = tf_course/tf_overall
keywords[courseid] = get_sorted_results(term_importance)
if courseid == '74953':
for word in keywords[courseid]:
print('has importance',term_importance['word'])
return keywords
解决方法
我能够弄清楚。看来我不再需要myfuncts.myfuction()
来加载just .myfuction()
我更改了此
from myfuncs import *
f = open('s2-titles.txt',encoding="utf8")
titles_lines = f.readlines()
f.close()
bag_of_words = myfuncs.get_bag_of_words(titles_lines)
keywords = myfuncs.get_keywords(titles_lines,bag_of_words)
对此有效并且有效引发了另一个错误,但这完全是另外一回事了。让我搜索SO以找出答案。
from myfuncs import *
f = open('s2-titles.txt',encoding = "utf8")
titles_lines = f.readlines()
f.close()
bag_of_words = get_bag_of_words(titles_lines)
keywords = get_keywords(titles_lines,bag_of_words)
---------------------------------------------------------------------------
KeyError Traceback (most recent call last)
<ipython-input-18-916e24603531> in <module>
5 f.close()
6
----> 7 bag_of_words = get_bag_of_words(titles_lines)
8 keywords = get_keywords(titles_lines,bag_of_words)
~\django\nlp-notebooks\myfuncs.py in get_bag_of_words(titles_lines)
13 bag_of_words[word] = course_bag_of_words[word]
14 else:
---> 15 bag_of_words[word] += course_bag_of_words[word]
16 return bag_of_words
17
KeyError: 'learning'
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。