python - 导入功能失败,但导入功能测试有效
问题描述
我使用 myfuncs 导入我的函数,但它失败了我确实测试了相同的文件路径,只是文件名略有不同,但它失败了。python 代码已有 5 年历史,不确定是否发生了变化。
我确实去了这个线程,我做了测试,它确实成功地完成了测试,我的实际外观是一样的。
#!/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 的文件中调用函数的代码
运行.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)
对此,它的工作原理会引发另一个错误,但那是另一回事了。让我现在搜索一下。
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'
推荐阅读
- angular - Reference.push 失败:第一个参数在属性“Questions.code”中包含未定义
- python - TypeError:get() 为参数“task_id”获取了多个值
- arrays - 我正在尝试做一个自定义 UITableViewCell 但我无法在桌子上显示结果
- reactjs - 如何为 textarea 街道地址启用自动填充?
- html - 电子 > 这是一个错误吗?
- javascript - React 如何在组件视图中打印 PDF 文件
- azure - Azure DevOps (On Premise) - Minimatch Pattern 下载工件的多个部分
- c# - 从字符串中获取特定部分
- c++ - 随着时间的推移确定分配调用站点(代码行)
- javascript - 寻求帮助以从 .filter() 获得正确结果