Python Forum
UnboundLocalError: local variable 'wmi' referenced before assignment
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
UnboundLocalError: local variable 'wmi' referenced before assignment
#1
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()
Reply
#2
(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.
BashBedlam likes this post
Reply
#3
... 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()
ibreeden likes this post
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  creating arbitrary local variable names Skaperen 9 2,946 Sep-07-2024, 12:12 AM
Last Post: Skaperen
  how solve: local variable referenced before assignment ? trix 5 2,548 Jun-15-2024, 07:15 PM
Last Post: trix
  It's saying my global variable is a local variable Radical 5 10,716 Oct-02-2023, 12:57 AM
Last Post: deanhystad
  local varible referenced before assignment SC4R 6 4,259 Jan-10-2023, 10:58 PM
Last Post: snippsat
  How does UnboundLocalError work? deanhystad 3 3,093 Feb-25-2022, 01:21 AM
Last Post: bowlofred
  Referenced before assignment finndude 3 4,880 Mar-02-2021, 08:11 PM
Last Post: finndude
  exec + subprocess = UnboundLocalError paul18fr 6 5,793 Feb-04-2021, 06:27 AM
Last Post: Gribouillis
  ReferenceError: weakly-referenced object no longer exists MrBitPythoner 17 19,492 Dec-14-2020, 07:34 PM
Last Post: buran
  Assignment of non-existing class variable is accepted - Why? DrZ 6 8,432 Jul-13-2020, 03:53 PM
Last Post: deanhystad
  UnboundLocalError: local variable 'figure_perso' referenced before assignment mederic39 2 3,663 Jun-11-2020, 12:45 PM
Last Post: Yoriz

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020
This forum uses Lukasz Tkacz MyBB addons.
Forum use Krzysztof "Supryk" Supryczynski addons.