python'module'对象没有属性'server_order'错误

时间:2013-12-03 13:01:50

标签: python python-2.7

我正在编写python。我有两个不同的python文件(client_side.py,server_side.py)调用

两个脚本功能。运行这些脚本后,我收到了这个错误。

'module' object has no attribute 'server_order' error

1。 Server_side.py

#!/usr/bin/python
import client_side

username = "xxxxxx"
password = "123"
filename = "dfsdf.txt"
client_side.client(username,password,filename)

def server_order():
    print "server side is running."
    return 

2。 client_side.py

#!/usr/bin/python

def client(a,b,c):
    print "client side function processing.."
    client_order()
    if __name__ == '__client__':
        client()
        return 

def client_order():
    import server_side
    server_side.server_order()
    return

1 个答案:

答案 0 :(得分:2)

问题在于您定义函数的顺序。您必须在导入之前定义这些函数,请尝试以下操作:

#!/usr/bin/python
import client_side

username = "xxxxxx"
password = "123"
filename = "dfsdf.txt"

def server_order():
    print "server side is running."
    return 

client_side.client(username,password,filename)

#!/usr/bin/python

def client_order():
    import server_side
    server_side.server_order()
    return

def client(a,b,c):
    print "client side function processing.."
    client_order()
    if __name__ == '__client__':
        client()
        return 

您最初的问题是在定义client_side.client()之前调用了server_order()。因此当client_side.client()试图调用它时,它就不存在了!