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()? Commented 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. Commented Dec 16, 2013 at 17:58

2 Answers 2

2

Do

import code.Giraffes

before executing the offending line:

userGiraffe = code.Giraffes.Giraffes.createGiraffe()
Sign up to request clarification or add additional context in comments.

6 Comments

Shouldn't importing "code" do all the work for that? Or do I have to import Giraffe separately also?
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")
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).
@JonathanSpirit No, you need to import submodules separately.
I'm glad to know it was helpful. Please accept the answer as correct if you are happy with it.
|
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 Comment

"code" is my package. In it is the file "Giraffes" with the class "Giraffes" with the function "createGiraffe".

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.