0

I import my function using myfuncs but it fails I did test the same file paths only slightly different file name but it fails. The python code was 5 years old not sure if something changed or not.

I did go to this thread and I did the test and it did works successfully for the test and my actual looks to be the same.

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)

This test works however my actual function file does not both files are in the same directory both imports are from import * same format just slightly different file names which should not matter.

the test to see if import works

from myfunction import *

pyth_test(1,2)

This works successfully

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

Code Below is what I run to call the function from the file called 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


enter image description here

john taylor
  • 1,080
  • 15
  • 31
  • 1
    If you use the `from myfuncs import *` form then `myfunc` names are directly accessible in local namespace without `myfuncs` prefix. Use `import myfuncs` alone, and use the prefix, or use only the name of the func. – progmatico Aug 30 '20 at 19:09
  • Thanks, that helps to understand the why part. Just started importing my own functions was only importing lib and others codes before. So I can with `import myfuncs` and use .prefix or local `from myfunc import` and not. Cool very clear to understand thanks again. – john taylor Aug 30 '20 at 19:16
  • Using the prefix is preferred over the wildcard, to avoid local namespace pollution with many names. But you can also be explicit and restrict the names to those you want to import. If you are starting with your modules beaware not to use module names already used in the standard library. Local modules are loaded first, and that kind of errors manifest themselves in weird ways. – progmatico Aug 30 '20 at 19:24
  • That is good to know. I personally I wanted to keep it all in one file. But was following along to the teacher that was changing all to functions and importing them. I was getting so confused I follow his style for the temp. I plan on doing that later as it was getting hard to keep it in one and follow along. But will remember if I do want to, i should use a unique name convention thing like jtf_myfunction. Sounds like you can also then override functions with your own custom ones, not that anything I write will be better in the near term. – john taylor Aug 30 '20 at 19:34
  • Establishing our own well intended naming conventions is not the best option, because while it may work, many others faced the same problem and already established sane good conventions every Python practitioner follows. This makes code bases more regular and easier to read for everyone. – progmatico Aug 31 '20 at 18:36
  • 1
    Just check the name you choose against [this list](https://docs.python.org/3/py-modindex.html). And follow the PEP8 guidelines [here](https://www.python.org/dev/peps/pep-0008/). If you find it to be too much reading just focus [here](https://www.python.org/dev/peps/pep-0008/#package-and-module-names) for now. – progmatico Aug 31 '20 at 18:41
  • 1
    If you follow PEP8 style there are even style check tools you can use to check your code style, or even style the code for you.. Of course this may not make sense in the classroom. But if you continue to code in Python, PEP8 is important to know. – progmatico Aug 31 '20 at 18:48

1 Answers1

0

I was able to figure it out. It looks like i don't need the myfuncts.myfuction() for it to load anymore just .myfuction()

I changed this

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)

Into this and it works throws another error but that's a whole other story. for me to search around SO to figure out now.

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'
john taylor
  • 1,080
  • 15
  • 31