Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
UnBoundLocalError
#1
Hello to everyone!

I'm trying to create a code using Python but I have a problem.
It gives me this error:
Error:
File "/opt/miniconda3/bin/sfipick", line 268, in <module> image = rsf2image(0) File "/opt/miniconda3/bin/sfipick", line 67, in rsf2image return img UnboundLocalError: local variable 'img' referenced before assignment
I will post here the part of the code where the problem is:
def rsf2image(i3):
    global byte
    ppm = '%s%d.ppm' % (name,i3)
    if not os.path.isfile(ppm):
        command = '< %s %s n3=1 f3=%d | %s %s | %s %s > %s' % \
            (byte,sfwindow,i3,sfgrey,args,ppmpen,args,ppm)
        if os.system(command) or not os.path.isfile(ppm):
            sys.stderr.write('Failed to execute "%s"\n\n' % command)
            sys.exit(3)
        ppms.append(ppm)
    img = PhotoImage(file=ppm)
    return img

...
image = rsf2image(0)
Can someone please help me with this?

Thank you very much
Reply
#2
The error is suggesting that you are calling img before assigning it on line 67.
I welcome all feedback.
The only dumb question, is one that doesn't get asked.
My Github
How to post code using bbtags


Reply
#3
(May-30-2020, 09:45 AM)menator01 Wrote: The error is suggesting that you are calling img before assigning it on line 67.
He defines img on line 66

(May-30-2020, 08:59 AM)Seaninho Wrote: Hello to everyone!

I'm trying to create a code using Python but I have a problem.
It gives me this error:
Error:
File "/opt/miniconda3/bin/sfipick", line 268, in <module> image = rsf2image(0) File "/opt/miniconda3/bin/sfipick", line 67, in rsf2image return img UnboundLocalError: local variable 'img' referenced before assignment
I will post here the part of the code where the problem is:
def rsf2image(i3):
    global byte
    ppm = '%s%d.ppm' % (name,i3)
    if not os.path.isfile(ppm):
        command = '< %s %s n3=1 f3=%d | %s %s | %s %s > %s' % \
            (byte,sfwindow,i3,sfgrey,args,ppmpen,args,ppm)
        if os.system(command) or not os.path.isfile(ppm):
            sys.stderr.write('Failed to execute "%s"\n\n' % command)
            sys.exit(3)
        ppms.append(ppm)
    img = PhotoImage(file=ppm)
    return img

...
image = rsf2image(0)
Can someone please help me with this?

Thank you very much
Is this your full code? The problem might be somewhere else too...
pyzyx3qwerty
"The greatest glory in living lies not in never falling, but in rising every time we fall." - Nelson Mandela
Need help on the forum? Visit help @ python forum
For learning more and more about python, visit Python docs
Reply
#4
You can solve UnboundLocalError by changing the scope of the variable that complains. You need to explicitly declare the global variable. The scope of the variable x in the printx function is global. You can verify the same by printing the value of x in the terminal and it will be 6.
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  How does UnboundLocalError work? deanhystad 3 1,633 Feb-25-2022, 01:21 AM
Last Post: bowlofred
  UnboundLocalError: local variable 'wmi' referenced before assignment ilknurg 2 1,855 Feb-10-2022, 07:36 PM
Last Post: deanhystad
  exec + subprocess = UnboundLocalError paul18fr 6 3,441 Feb-04-2021, 06:27 AM
Last Post: Gribouillis
  UnboundLocalError: local variable 'figure_perso' referenced before assignment mederic39 2 2,227 Jun-11-2020, 12:45 PM
Last Post: Yoriz
  UnboundLocalError: local variable referenced before assignment svr 1 3,260 Dec-27-2019, 09:08 AM
Last Post: perfringo
  UnboundLocalError: local variable ' ' referenced before assignment d3fi 10 5,430 Sep-03-2019, 07:22 PM
Last Post: buran
  'Exception Has occured: UnBoundLocalError' caston 1 2,413 Jun-12-2019, 02:33 PM
Last Post: perfringo
  UnboundLocalError Problem DrChicken24 1 2,186 Mar-27-2019, 02:53 AM
Last Post: ichabod801
  UnboundLocalError, how to fix it please? etrius 4 3,838 Jan-18-2018, 09:53 PM
Last Post: nilamo
  How to fix UnboundLocalError Tawnwen 5 5,269 Jan-07-2018, 05:38 PM
Last Post: Gribouillis

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020