从Python中的另一个文件调用函数

时间:2013-12-16 17:48:58

标签: python function class

是的,之前已经问过这个问题。不,它没有令人满意地回答我的问题。 所以,我首先用Python创建我的长颈鹿程序(不要问),我试图让用户命名他们的长颈鹿。

我的文件都在一个名为code的包中。在文件Main_Code中,函数createGiraffecode.Various_Functions调用。这是我的代码。

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

代码中的代码.Giraffes:

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

但是,当我运行Main_Code时,它会给我这个错误:

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'

我该如何解决这个问题?我相信我已经完成了这本书。它都使用相同的包,所以我可以调用该函数,我从正确的位置调用它,并且该函数没有语法错误。任何人都可以帮忙吗?

2 个答案:

答案 0 :(得分:2)

待办事项

import code.Giraffes
在执行违规行之前

userGiraffe = code.Giraffes.Giraffes.createGiraffe()

答案 1 :(得分:0)

当您调用以下函数时:

userGiraffe = code.Giraffes.Giraffes.createGiraffe()

这意味着您在dir code中有一个项目,内部目录Giraffes和模块Giraffes,功能createGiraffe。这是你的例外吗?

相关问题