来自新文件的属性错误Python

时间:2017-08-26 15:43:35

标签: python python-3.x

我在python中遇到属性错误,但我已经导入了我需要的所有内容。这是令人困惑的,因为这似乎是合乎逻辑的,因为没有任何东西或命令,我不认为虽然我说的是来自java背景。

这是一个名为utils.py

的文件
import runMain


def isUser(credentialsInput):
    return credentialsInput in runMain.users


def isReserved(names):
    for strings in runMain.roomName:
        if names in strings:
            return True
        else:
            return False


def getIndex(e, check):
    return e.index(check)


def findInArray(e, searchName):
    runMain.i = 0
    while runMain.i < len(e):
        test = e[runMain.i]
        if searchName in test:
            break
    return runMain.i


def registerUser(creds):
    runMain.users.__add__(len(runMain.users), creds)


def runCredentialCheck():
    runMain.username = input("admin$ -u>>> ")
    runMain.password = input("admin$ -p>>> ")
    runMain.credentials = runMain.username + ":" + runMain.password
    return isUser(runMain.credentials)

我有另一个名为runMain的文件,其中包含以下代码:

import utils
import sys


TOTAL_ROOMS = 500
rooms = []
suite = []
reservations = []
reservationParts = []
roomNum = 0
suiteOut = ""
suiteF = ""
name = ""
userInput = ""
suiteT = " and is a suite"
utils.users.append("foo:hello")


userInput = input(">>> ")
while True:
        if userInput == "new -r":
                utils.username = input("admin$ -u>>> ")
                utils.password = input("admin$ -p>>> ")
                utils.credentials = utils.username + ":" + utils.password
                if utils.isUser(credentialsInput=utils.credentials):
                        userInput = input("rsr# -n ")
                        reservationParts = userInput.split()
                        roomNum = reservationParts[0]
                        name = reservationParts[1]
                        if name in rooms:
                                print(">>> Room is occupied")
                        elif name in utils.roomName:
                                print(">>> Room is occupied")
                        rooms.append(roomNum)
                        utils.roomName.append(name)
                        utils.loopCount = 0
                        if "--suite" in userInput:
                                suite.append(True)
                                suiteOut = suiteT
                        else:
                                suite.append(False)
                                suiteOut = suiteF
                        reservations.append("Room " + roomNum + " is filled by " +
                                            utils.roomName.__getitem__(utils.loopCount) + suiteOut)
                        for ints in rooms:
                                if suite.__getitem__(utils.loopCount):
                                        suiteOut = suiteT
                                else:
                                        suiteOut = suiteF
                                sys.stdout.write("Room " + ints + " is filled by " +
                                                 utils.roomName.__getitem__(utils.loopCount) + suiteOut)
                                print()
                                utils.loopCount += 1
                        userInput = input(">>> ")
                        if userInput == "quit()":
                                break
                else:
                        print("Invalid Credentials")
        elif userInput == "new -u":
                if utils.runCredentialCheck():
                    utils.username = input("new -u -u>>> ")
                    utils.password = input("new -u -p>>> ")
                    utils.registerUser(creds=(utils.username + ":" + utils.password))
                else:
                    print("Invalid Credentials")
                userInput = input(">>> ")

我不确定我做错了什么。我收到错误: AttributeError:模块'utils'没有属性'runCredentialCheck'。

1 个答案:

答案 0 :(得分:1)

您的代码已

  • 循环导入循环(runMain导入utils,导入导入的runMain ...)
  • 包含导入模块时运行的(非定义)代码的模块

这两种都是不良做法,部分是因为它们组合起来会破裂。循环模块依赖通常不是问题,因为Python只运行每个模块中的代码一次。但是,有一种情况是Python可以运行它两次:在命令行上命名模块时。

# foo.py
import bar
print("in foo")
# bar.py
import foo
print("in bar")
$ python3 foo.py
in foo
in bar
in foo

要解决此问题,您应该打破依赖关系:如果您需要utils来访问runMain中的项目,请将其移至utils或将其放入第三个模块中utilsrunMain包括;并使用runMain.py语句保护if内的代码,该语句仅在直接从命令行运行文件时运行:

if __name__ == "__main__":
    # code here will be executed only when the module is run directly

这是一种常见的Python习语。

顺便说一句,我还没有尝试理解你的所有代码,但utils中的某些作业声明可能不会做你想要的,因为它们会影响&#34 ;内&#34; runMain模块,而不是&#34;外部&#34;图层(以__main__而不是runMain运行)。解决此问题的最佳方法是不要使用循环模块依赖项,但如果这样做,则不应直接执行循环中的任何模块。