Starting on January 1, 2020, the Python community will no longer update, fix bugs, or patch security issues for Python 2.7. We recommend that you migrate apps that are still running in the Python 2 runtime of the App Engine standard environment to the Python 3 runtime as soon as possible. For more information, see Python 2 support on Google Cloud.
Key differences between the Python 2 and Python 3 runtimes
Most of the changes you need to make during the migration come from the following differences between the Python 2 and Python 3 runtimes:
- Compatibility issues between Python 2 and Python 3
- No bundled App Engine services in the Python 3 runtime
- A web framework is required to route requests for dynamic content
Compatibility issues between Python 2 and Python 3
When Python 3 was first released in 2008,
several backward incompatible changes
were introduced to the language. Some of these changes require only minor updates
to your code, such as changing the print
statement to a print()
function.
Other changes may require significant updates to your code, such as updating
the way that you handle binary data, text, and strings.
Many popular open source libraries, including the Python standard libraries, also changed when they moved from Python 2 to Python 3.
No bundled App Engine services in the Python 3 runtime
Starting with the Python 3 runtime, the App Engine standard environment no longer includes bundled App Engine services such as Memcache and Task Queues. Instead, Google Cloud provides standalone products that are equivalent to most of the bundled services in the Python 2 runtime. For the bundled services that are not available as separate products in Google Cloud, such as image processing, search, and messaging, you can use third-party providers or other workarounds as suggested in this migration guide.
Removing the bundled App Engine services enables the Python 3 runtime to support a fully idiomatic Python development experience. In the Python 3 runtime, you write a standard Python app that is fully portable and can run in any standard Python environment, including App Engine.
Web framework required to route requests for dynamic content
In the Python 2 runtime, you can create URL handlers in the app.yaml
file
to specify which app to run
when a specific URL or URL pattern is requested.
In the Python 3 runtime, your app needs to use a web framework such as
Flask or Django to route requests for dynamic content instead of using
URL handlers in app.yaml
. For static content, you can continue to create URL
handlers
in your app's app.yaml
file.
Overview of the migration process
We recommend the following incremental approach to the migration, in which you maintain a functioning and testable application throughout the process:
Migrate the App Engine bundled services in your Python 2 app to Google Cloud services, third-party services, or other recommended replacements. Test your app after migrating each service.
When you finish this step, your Python 2 app should no longer use any
google.appengine
APIs.Upgrade your app to be compatible with both Python 2 and Python 3.
Several solutions are available to help with this upgrade. For example, use Python-Future or Python-Modernize.
For more information about this step of the migration process, see Porting Python 2 Code to Python 3 on the Python Software Foundation documentation site.
Prepare App Engine configuration files for the Python 3 runtime. Several important changes affect the configuration settings in
app.yaml
, including but not limited to:- Apps are now assumed to be threadsafe. If your app isn't threadsafe, you
need to make some changes in your
app.yaml
file to avoid the potential for significantly increased costs. - The
app.yaml
file no longer routes requests to your scripts. Instead, you are required to use a web framework with in-app routing, and update or remove allscript
handlers inapp.yaml
.
- Apps are now assumed to be threadsafe. If your app isn't threadsafe, you
need to make some changes in your
Test and deploy your upgraded app in a Python 3 environment.
You may need to spend significant time fixing compatibility issues that aren't visible until you run your upgraded app in a Python 3 environment.
After all of your tests pass, deploy the upgraded app to App Engine but prevent traffic from automatically routing to the new version. Use traffic splitting to slowly migrate traffic from your app in the Python 2 runtime to the app in the Python 3 runtime. If you run into problems, you can route all traffic to a stable version until the problem is fixed.