webapp2 Web Framework
webapp2 is a lightweight Python web framework, designed specifically for Google App Engine's Python 2 standard environment. It extends the original `webapp` framework, providing enhanced routing, session management, and other utilities. The current version is 2.5.2, but the library is no longer actively maintained and does not support Python 3.
Common errors
-
ImportError: No module named webapp2
cause The webapp2 library is not installed in your current Python environment or is not found by the Google App Engine development server.fixRun `pip install webapp2` in your environment. If using Google App Engine, ensure `libraries:` section in `app.yaml` is correctly configured or that `webapp2` is in your `lib` directory for vendored libraries. -
500 Internal Server Error (when running a webapp2 application)
cause This is a generic error indicating an unhandled exception occurred within your webapp2 `RequestHandler` method (e.g., `get`, `post`). The actual traceback is hidden by default in production.fixSet `debug=True` in your `webapp2.WSGIApplication` initialization (e.g., `debug=True`) to see the full traceback in development. Log errors extensively and use `try...except` blocks in your handlers to catch and handle specific exceptions. -
TypeError: unsupported operand type(s) for +: 'unicode' and 'str' (or similar Unicode/byte string errors)
cause In Python 2, mixing `str` (byte strings) and `unicode` objects without explicit encoding/decoding operations can lead to `TypeError` or `UnicodeEncodeError`/`UnicodeDecodeError`.fixEnsure consistent handling of string types. For web responses, always encode text to a byte string (e.g., `my_unicode_string.encode('utf-8')`) or ensure all output is unicode. For input, decode byte strings to unicode (e.g., `request.get('param').decode('utf-8')`). Use `u'...'` literals for unicode strings.
Warnings
- breaking webapp2 is exclusively compatible with Python 2. It does not support Python 3, making it unsuitable for modern Python development environments. Attempts to run it on Python 3 will result in syntax errors or compatibility issues.
- breaking The webapp2 library is no longer actively maintained. This means there will be no new features, bug fixes, or security patches. Using unmaintained software can pose significant security risks and lead to unresolvable issues.
- gotcha webapp2 is tightly coupled with the Google App Engine Python 2 Standard Environment. Running it outside this environment, especially without proper WSGI server setup (like `gunicorn` with a custom server wrapper or the GAE dev server), can be complex and is not its primary use case.
Install
-
pip install webapp2
Imports
- RequestHandler
import webapp2
- WSGIApplication
import webapp2
Quickstart
import webapp2
class MainPage(webapp2.RequestHandler):
def get(self):
self.response.headers['Content-Type'] = 'text/plain'
self.response.write('Hello, webapp2!')
app = webapp2.WSGIApplication([
('/', MainPage),
], debug=True)
# To run locally with GAE dev server:
# In your app.yaml (or similar) point to this file:
# handlers:
# - url: /.*
# script: main.app