Ex40 module 'mystuff' has no attribute 'tangerine'

Hi there!

I am running the following example from “Learn Python 3 the Hard Way”

def apple():
    print("I AM APPLES!") 

# this is just a variable
tangerine="Living reflection of a dream"

I did put this into mystuff.py and did the following

import mystuff

mystuff.apple()
print(mystuff.tangerine)

However, I got an error saying that

module 'mystuff' has no attribute 'tangerine'

So basically, I can not simply define a variable inside “mystuff.py” and import it easily as written in the book.

I am using Python 3.8 by the way. Could you please let me know if this was an update of the python grammar or something else? Thank you.

Ah, I figured it out. It seems that the previous version I created of mystuff.py that does not contain the line tangerine="Living reflection of a dream"
is somehow still in the memory, while the new mystuff.py is not loaded… I quit Python and restarted, and now things work.

One solution to reload the module is to run the following:

import importlib
importlib.reload(mystuff)

I think this is important in the book as both versions are presented in the examples and I suspected people may have encountered similar errors as I did (if they are using iPython).

Oh are you doing this from within the python shell, or running it on the command line like:

python mystuff.py

If you’re doing it inside the python shell then, yes, it’ll only load the module once. If you run it from the command line then no problem at all since Python exits each time the script ends.

1 Like