Python 导入函数失败,但导入函数测试有效

Python 导入函数失败,但导入函数测试有效,python,python-3.x,python-import,Python,Python 3.x,Python Import,我使用myfuncs导入函数,但失败了我确实测试了相同的文件路径,只是文件名略有不同,但失败了。python代码使用了5年,不确定是否有更改 我确实去了这个线程,我做了测试,测试成功了,我的实际外观是一样的 这个测试工作,但我的实际功能文件不都在同一个目录中两个导入都来自导入*相同的格式只是稍微不同的文件名,这应该不重要 检查导入是否有效的测试 from myfunction import * pyth_test(1,2) 这是成功的 3 myfuncs.py #!/usr/bin/en

我使用myfuncs导入函数,但失败了我确实测试了相同的文件路径,只是文件名略有不同,但失败了。python代码使用了5年,不确定是否有更改

我确实去了这个线程,我做了测试,测试成功了,我的实际外观是一样的

这个测试工作,但我的实际功能文件不都在同一个目录中两个导入都来自导入*相同的格式只是稍微不同的文件名,这应该不重要

检查导入是否有效的测试

from myfunction import *

pyth_test(1,2)
这是成功的

3
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'
从myfuncs导入*
f=打开('s2-titles.txt',encoding=“utf8”)
标题_行=f.阅读行()
f、 关闭()
单词袋=单词袋(标题行)
关键词=获取关键词(标题行、单词袋)
---------------------------------------------------------------------------
KeyError回溯(最近一次呼叫最后一次)
在里面
5 f.关闭()
6.
---->7包单词=获取包单词(标题行)
8个关键词=获取关键词(标题行、单词袋)
~\django\nlp notebooks\myfuncs.py在一袋字(标题行)中
13包单词[单词]=当然包单词[单词]
14.其他:
--->15袋单词[单词]+=当然袋单词[单词]
16返回一袋单词
17
关键错误:“学习”

如果使用myfuncs导入*表单中的
,则可以在本地命名空间中直接访问
myfuncs
名称,而无需
myfuncs
前缀。单独使用
import myfunc
,并使用前缀,或仅使用func的名称。谢谢,这有助于理解为什么部分。刚开始导入我自己的函数之前只导入lib和其他代码。因此,我可以使用
导入myfunc
并使用.prefix或local
从myfunc导入而非导入。很酷很清楚明白再次感谢。使用前缀比通配符更可取,以避免本地名称空间对许多名称的污染。但您也可以显式地将名称限制为要导入的名称。如果开始使用模块,beaware将不使用标准库中已使用的模块名称。首先加载本地模块,这种错误会以奇怪的方式表现出来。只需检查您选择的名称即可。并遵循PEP8指南。如果你觉得阅读太多,现在就关注它。如果你遵循PEP8风格,甚至可以使用风格检查工具来检查你的代码风格,甚至为你设计代码风格。。当然,这在课堂上可能没有意义。但是,如果您继续使用Python编写代码,那么了解PEP8是很重要的。
#!/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 *

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'