【发布时间】:2020-08-30 18:37:58
【问题描述】:
我使用 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)
这个测试有效,但是我的实际函数文件不是两个文件都在同一个目录中,两个导入都来自导入 * 相同的格式只是文件名略有不同,这无关紧要。
检查导入是否有效的测试
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
【问题讨论】:
-
如果您使用
from myfuncs import *形式,则myfunc名称可以在本地命名空间中直接访问,无需myfuncs前缀。单独使用import myfuncs,并使用前缀,或者只使用func的名称。 -
谢谢,这有助于理解为什么部分。刚开始导入自己的函数之前只导入了lib和其他代码。所以我可以使用
import myfuncs并使用 .prefix 或本地from myfunc import而不是。很酷很清楚,再次感谢。 -
使用前缀优先于通配符,以避免本地命名空间污染与许多名称。但是您也可以明确地将名称限制为您要导入的名称。如果您从模块开始,请注意不要使用标准库中已使用的模块名称。本地模块首先加载,这种错误以奇怪的方式表现出来。
-
如果你遵循 PEP8 风格,甚至可以使用风格检查工具来检查你的代码风格,甚至可以为你设计代码风格。当然这在课堂上可能没有意义。但是,如果您继续使用 Python 编写代码,那么了解 PEP8 很重要。
标签: python python-3.x python-import