3

I've been building a web application for the past month, and things have been going surprisingly smoothly for how new I am to all of this. That is, until yesterday, when I did an AJAX overhaul on a page to provide users with live feedback of a server-side process. Now when I boot up the application, I get an error on my first line of code (which has produced no problems up to this point) where I check the session object to see if an operation is active.

I am getting the error:

UnboundLocalError: local variable 'session' referenced before assignment

when I try the code:

if 'active_op' not in session:
    print 'bizbaz'

I would have thought that I was doing something wrong with this code if it hadn't worked seemlessly before. I've reverted my code to what I had before the AJAX update, and I'm still getting this error, even though it worked yesterday with the same code. Any pointers on this would be greatly appreciated. As I said, I'm still kind of new to web application development and sessions, in particular. Apparently my understanding of sessions and flask's use of them is a bit off, because I'm completely stumped as to why this has suddenly stopped working. Let me know if there's anything I can do to clarify my issue; I'm stuck until I get this figured out. Thanks!

EDIT: I had this code which was trying to assign a dictionary to the session variable.

session = {'foo':'bar'}
3
  • You're going to need to show more code. If for example session is a module level variable but you're assigning to it anywhere in the function, you'll get this error if you attempt to read it before the assignment. Commented Mar 6, 2014 at 18:32
  • Thanks @DanielRoseman. That was the problem. I did a search and replace to clean up some of my code, and I missed a spot where session was assigned to a dict. Commented Mar 6, 2014 at 18:56
  • I edited your question to include this information about "session assigned to a dict". I will also edit the answer from Sergey to mention this which should complete the answer Commented Mar 6, 2014 at 20:44

1 Answer 1

6

Make sure you import session before you use it

from flask import session

EDIT

The main problem was that you were assigning a dictionary to the 'session' variable which you should not do.

# Incorrect
session = {'foo':'bar'}

Instead, it should be

session['foo'] = 'bar'
Sign up to request clarification or add additional context in comments.

8 Comments

I did. I'm working on a fix right now; something I missed earlier, I'll let you know if it solves this.
No luck. Really not sure what's causing this. I pulled some code from a few days ago, and that is working with the session in the same place, as expected.
I am correct in assuming that session exists once it's imported, though, right? I don't need to initialize it or anything before I can run a check like this? It's really throwing me that it worked before, and I can't seem to find any discernible difference that would make it behave like a 'local variable'.
It's at 666 LOC now; maybe that's the problem :p
May not need to: If I were to have inadvertently assigned a value to 'session' at a later point in the code, would python flag this like it has? There was some pretty cavalier search and replacing as part of this AJAX update, and I think I missed a little spot where the key value was dropped off of session and it is just assigned as: session = {'foo': 'bar'}
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.