访问 trycatch 块之外的变量 - Python

时间:2021-07-05 14:12:18

标签: python try-catch

我在一个文件中有一个 try-catch 块,我想在另一个文件中使用该变量的值。

我有这个:-

next()

但是当我这样做时


try:
    cnx = connect(user=database_username, password=database_password,
                  host=database_hostname,
                  database=database_name)
    cursor = cnx.cursor()
except mysql.connector.Error as err:
    if err.errno == errorcode.ER_ACCESS_DENIED_ERROR:
        print("Something is wrong with your user name or password")
    elif err.errno == errorcode.ER_BAD_DB_ERROR:
        print("Database does not exist")
    else:
        print(err)
else:
    cnx.close()


我明白


from database import cursor

我不知道会发生这种情况,因为 ImportError: cannot import name 'cursor' from 'database' 是异步的并且在 try-catch 块内

问题是我该如何解决?

0 个答案:

没有答案
相关问题