I was trying to change the os.environ dict to simulate a logged in user on Google App Engine, as documented at https://stackoverflow.com/a/6230083/1241454.
from google.appengine.api import users
import webapp2
import os
class TestPage(webapp2.RequestHandler):
def get(self):
os.environ['USER_EMAIL'] = 'a@b.c'
user = users.get_current_user()
self.response.out.write(user.email())
This doesn't work. get_current_user()
returns None in the above example for me, at least when running on the dev server. I get the same result when using testbed.setup_env()
rather than directly editing os.environ. However, the below does work:
from google.appengine.api import users
import webapp2
import os
class TestPage(webapp2.RequestHandler):
def get(self):
os.environ['USER_EMAIL'] = 'a@b.c'
reload(users)
user = users.get_current_user()
self.response.out.write(user.email())
The only change was reloading the users module after changing os.environ. Is this expected behavior, or is something wrong with my App Engine set up? My understanding is that Python / App Engine should load only one copy of the os module loaded into the system, not two.
Any ideas? This is very confusing to me.