Python Forum

Full Version: UnboundLocalError: local variable 'wmi' referenced before assignment
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
I have the code below. When i run it, i get this error

UnboundLocalError: local variable 'wmi' referenced before assignment

How can i solve it?


import os
import mysql.connector
from mysql.connector.constants import ClientFlag
import json
from datetime import datetime
import wmi_client_wrapper as wmi
import base64


exec(open("/etc/epp/SNMP/mysqlconnector.py").read())

wmi_username = " "
wmi_password = " "
wmi_hostname = " "


def wmi_access():
    query = "select wmi_username, wmi_password, wmi_hostname from wmi_account where status = 'enable'"
    cursor = connection.cursor(dictionary=True)
    cursor.execute(query)
    usrnme = cursor.fetchall()
    for data in usrnme:
        username = data['wmi_username']
        password = data['wmi_password']
        password_active = base64.b64decode(password).decode("utf-8")
        wmi_hostname = data['wmi_hostname']

    wmi = wmi.WmiClientWrapper(
        username=wmi_username,
        password=password_active,
        host=wmi_hostname,
    )

wmi_access()
(Feb-10-2022, 12:37 PM)ilknurg Wrote: [ -> ]import wmi_client_wrapper as wmi
...
wmi = wmi.WmiClientWrapper(...)
You should not introduce a variable with the same name as the (alias of) the module you imported.
...
import wmi_client_wrapper as wmi
...
 
# OUT HERE wmi IS A MODULE
...
def wmi_access():
    ...
    # IN HERE wmi IS A LOCAL VARIABLE BECAUSE OF THIS LINE
    wmi = wmi.WmiClientWrapper(
        username=wmi_username,
        password=password_active,
        host=wmi_hostname,
    )
 
# AND NOW IT IS A MODULE AGAIN

wmi_access()