0

I'm using a 2010 Head First Book for Python, chapter 2. I've created a module called nester, which contains the function print_lol, then made another program which should import nester, create a little list, and then call the function print_lol contained in nester. It doesn't work, tho.

import nester

cast = ["Palin", "Cleese", "Idle", "Jones", "Gilliam", "and Chapman."]

nester.print_lol(cast)

This is the program, and this is the output:

> Traceback (most recent call last):
   File "<pyshell#2>", line 1, in <module>
   nester.print_lol(cast)
AttributeError: module 'nester' has no attribute 'print_lol'

What's wrong with that? Why this happens? The code is exactly as in the book, same path, environment paths are ok. What's wrong?

Here is the 'nester' code, and it works properly.

def print_lol(the_list):

for each_item in the_list:
    if isinstance(each_item, list):
        print_lol(each_item)    
    else:
            print(each_item)

Also, the nester it's in C:\nester. It contains setup.py, nester.py, and the installation folders and files: MANIFEST, Lib, dist, build.

Monok
  • 39
  • 1
  • 2
  • 10
  • Where are you running this program from? Are you sure the source from which your running it from has context of the `nester` module? Python is telling you it can't find a reference to `print_lol` from `nester`. – ospahiu Feb 10 '17 at 14:51
  • you should probably add the "nester" module to your question, maybe there is a problem with the function – a.smiet Feb 10 '17 at 14:51
  • 1
    Have you saved the module? Also, the interpreter might need to be restarted or you might need to explicitly `reload(nester)`. – Peter Wood Feb 10 '17 at 15:13
  • @mrdomoboto From the IDLE, opening the .py file with CTRL+O. – Monok Feb 10 '17 at 15:30
  • @PeterWood Where should I put 'reload(nester)'? – Monok Feb 10 '17 at 15:30

3 Answers3

1

Try this on your python console

dir("nester")

It should show all the available functions. You might need to make sure print_lol is in the list. Most likely, it is under some other sub-tribute. So the way you call it should be nester.some_tribute.print_lol()

ju.
  • 1,016
  • 1
  • 13
  • 34
  • It gives me a list of namespaces, then functions, but it can't find my print_lol! Not even after some tribute. – Monok Feb 10 '17 at 15:33
0

Since import nester didn't throw in error, that means your script can import nester. Try (explicit function import)

from nester import print_lol

If this fails, make sure print_lol exist in nester, as in there are no spelling mistakes.

Niyojan
  • 544
  • 1
  • 6
  • 23
  • It exist, it's defined, but even with your code it doesn't work. I added the nester code, may you check it out because maybe there's something wrong in it? – Monok Feb 10 '17 at 15:31
  • I tried your code and it worked for me. Can you make sure you are importing the right `nester`, put `print(dir(nester)` after `import nester`, share the result here (leave double underscore values). – Niyojan Feb 10 '17 at 16:04
  • ['__doc__', '__loader__', '__name__', '__package__', '__path__', '__spec__'] Plus, the AttributeError where module 'nester' has no attribute 'paint_lol' – Monok Feb 10 '17 at 16:09
  • add this `print(nester.__file__)` and confirm the path is same as `c:\nester\nester.py` (or wherever your `nester` module is. – Niyojan Feb 10 '17 at 16:14
  • It is! But I think the solution is about importing the class, and I'm actually importing the module – Monok Feb 10 '17 at 16:35
  • Damn, I was sure that you were importing the wrong `nester`, guess i was wrong. Anyway, its not about import a class, because even to do that you have to import the module in which the class exist. Your case is a simple function import from module which should have worked even by `import nester.print_lol`. – Niyojan Feb 10 '17 at 16:44
  • Let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/135400/discussion-between-niyojan-and-monok). – Niyojan Feb 10 '17 at 16:45
-1

I guess I found out the answer, and it was pretty easy, but I didn't notice that until deeper research on the website with two friends here.

The module where the 'nester' class is put it's called 'nester'. So when I tried to import 'nester', I imported the module, not the class. I changed the code from the question one to:

from nester import nester

cast = ["Palin", "Cleese", "Idle", "Jones", "Gilliam", "and Chapman."]

nester.print_lol(cast)

So I import from 'nester' Module the 'nester' Class. Pro-tip: NEVER use the same name for Module and Class.

Credits:

Community
  • 1
  • 1
Monok
  • 39
  • 1
  • 2
  • 10
  • When you said "Here is the 'nester' code" you never mentioned that this code is inside `nester` class. Follow naming conventions which says class names should be like `Nester` not `nester`. Makes it easy for others to understand your code. – Niyojan Feb 23 '17 at 09:16
  • I thought it was pretty obvious, but you're right about naming convention. I've just started learning tho, that's why I didn't put the capital letter. – Monok Feb 25 '17 at 00:43