0

Yes, this question has been asked before. No, it did not answer my question satisfactorily. So, I'm creating my Giraffe Program in Python first (don't ask) and I'm trying to get the user to name their giraffe.

My files are all in one package called code. In the file Main_Code, the function createGiraffe is called from code.Various_Functions. Here is my code.

import code
print("Welcome to The Animal Kingdom.")
userGiraffe = code.Various_Functions.createGiraffe()

And the code in code.Giraffes:

import code
def createGiraffe():
    print("Name your giraffe.")
    GiraffeName = input()
    return GiraffeName

However, when I run Main_Code, it gives me this error:

Traceback (most recent call last):
  File "C:\Users\Jonathan\Documents\Aptana Studio 3 Workspace\The Animal Kingdom\src\code\Main_Code.py", line 3, in <module>
    userGiraffe = code.Giraffes.Giraffes.createGiraffe()
AttributeError: 'module' object has no attribute 'Giraffes'

How do I fix this? I believe that I've done everything by the book. It's all using the same package so I can call the function, I'm calling it from the right place, and the function has no syntax errors. Can anyone help with this?

2
  • Why is the code in the traceback different to the code you have put in your question? One is code.Various_Functions.createGiraffe(), the other is code.Giraffes.Giraffes.createGiraffe(). And why not just code.Giraffes.createGiraffe()?
    – jonrsharpe
    Dec 16, 2013 at 17:53
  • Yeah, that was a messup. I copied the wrong error. The traceback should be code.Various_Functions.createGiraffe() which does create the same exact error. Dec 16, 2013 at 17:58

2 Answers 2

2

Do

import code.Giraffes

before executing the offending line:

userGiraffe = code.Giraffes.Giraffes.createGiraffe()
6
  • Shouldn't importing "code" do all the work for that? Or do I have to import Giraffe separately also? Dec 16, 2013 at 17:53
  • You have to import submodules separately. Try in IDLE: import os os.path.join("this","shouldn't","work"), then import os.path and os.path.join("This","should","work")
    – Adam Smith
    Dec 16, 2013 at 17:53
  • I see. Also, that was a mess-up with the error. I copied the error before I changed it to Various_Functions (which makes the same error). Dec 16, 2013 at 17:55
  • @JonathanSpirit No, you need to import submodules separately.
    – piokuc
    Dec 16, 2013 at 17:55
  • I'm glad to know it was helpful. Please accept the answer as correct if you are happy with it.
    – piokuc
    Dec 16, 2013 at 17:56
0

When you call function like:

userGiraffe = code.Giraffes.Giraffes.createGiraffe()

it means you have a project in dir code with internal dir Giraffes and module Giraffes with function createGiraffe. Is this your exception?

1
  • "code" is my package. In it is the file "Giraffes" with the class "Giraffes" with the function "createGiraffe". Dec 16, 2013 at 17:54

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.