0

Good day everyone!

I have a question regarding attribute errors since I can't find a solution and I have been trying to fix this for the past few hours.

First I defined a function called nesterNEW:

import sys

def print_lol(the_list, indent=False, level = 0, fh=sys.stdout):

    for each_item in the_list:
        if isinstance(each_item, list):
            print_lol(each_item, indent, level+1, fh)
        else:
                        if indent:
                                for tab_stop in range(level):
                                        print("\t", end = '', file=fh)
        print(each_item, file=fh)

I built the distribution and placed the distribution in the same folder as the following code:

man = []
other = []
import nesterNEW

try:
    data = open('sketch.txt')

    for each_line in data:
        try:
            (role, line_spoken) = each_line.split(':', 1)
            line_spoken = line_spoken.strip()
            if role =='Man':
                man.append(line_spoken)
            elif role == 'Other Man':
                other.append(line_spoken)
        except ValueError:
            pass
    data.close()
except IOError:
    print('file is missing')

print(man)
print(other)

try:       
    with open('man_data.txt', 'w') as man_file:
        nesterNEW.print_lol(man, file=man_file)
    with open('other_data.txt', 'w') as other_file:
        nesterNEW.print_lol(other, file=other_file)
except IOError as err:
    print('File Error: ' + str(err))

It keeps giving me this error message: AttributeError: module 'nesterNEW' has no attribute 'print_lol'

I have tried the following solution, but to no avail. I tried to import the submodules, with import nesterNEW.print_lol, but that didn't work.

Another possible solution I found is here. If you look at answer number 2, it says that the file name must be changed, if import nesterNEW.print_lol isn't working. So I tried check the directory with nesterNEW.__file__, but it threw another attribute error at me.

I have the feeling that I'm misinterpreting the solutions that are given on the linked sites.

This is my first time posting on here, so I hope I managed to use the right formatting. :)

Dan
  • 1
  • 1
    `...I defined a function called nesterNEW` did you mean you defined a function in the module `nesterNEW.py`? – wwii Mar 27 '18 at 14:39
  • 1
    Right after the `import nesterNEW` statement, print `dir(nesterNEW)` is your function in there? ... https://docs.python.org/3/library/functions.html#dir – wwii Mar 27 '18 at 14:45
  • @wwii, no it isn't there. – Dan Mar 27 '18 at 15:15
  • @Divibisan, if I just write `print_lol` it will tell me that it isn't defined – Dan Mar 27 '18 at 15:17
  • Are you sure that you successfully imported `nesterNew`? – divibisan Mar 27 '18 at 15:19
  • 1
    how many nesternew files do you got? you're not calling the good one – bobrobbob Mar 27 '18 at 15:22
  • @divibisan, when I write `import nesterNEW` in IDLE it just jumps to the next "line" without any error messages, so I am assuming that it's doing the correct thing. @bobrobbob, that's the only one I got – Dan Mar 27 '18 at 15:32
  • sorry i didn't test tour code sooner. i got `Type Error` when `nesterNEW.print_lol(man, file=man_file)` is called. the correct argument name is `fh` – bobrobbob Mar 27 '18 at 18:01
  • I tried changing it, but it didn't work. Maybe I changed the wrong one? I changed "file" into "fh" – Dan Mar 27 '18 at 18:41
  • If you are not getting an `ImportError` and `print_lol()` is not in the module that you imported then you must have more than one module named `nesterNEW.py` and the one being imported is incomplete. – wwii Mar 27 '18 at 18:45
  • Dan you got to change both @wwii i already said that, and i still think i'm right – bobrobbob Mar 27 '18 at 18:51
  • @wwii, how do I check if there is only one? Maybe I'm missing something. I checked the folder and the `nesterNEW.py` module in there opens up the code that I posted above. @bobrobbob, I changed it in both lines ("man" and "other") if that's what you mean. I am more or less completely new to programming, so sorry if it takes longer for me to understand certain things you guys are saying. – Dan Mar 27 '18 at 19:22
  • I should probably add that I got this problem from the book called "Head First Python" on page 127/128. – Dan Mar 27 '18 at 19:26
  • Is it possible that `nesterNEW.cpython-35.pyc` is causing the problem? – Dan Mar 27 '18 at 19:34
  • i don't think so, but you can delete it safely – bobrobbob Mar 27 '18 at 21:00
  • You were right, it still throws out the same error. Do you have any other suggestions on what I could do? – Dan Mar 28 '18 at 08:43
  • if your function really doesn't appear in `dir()` try again in a new folder – bobrobbob Mar 28 '18 at 22:43

0 Answers0