This sample is offered in Python 3, but the code itself is Python 2-compatible. Instructions on running it under 2.x will leverage the resources of the Cloud API Python sample which provides a more complete experience as it has all configuration available to support 2.x deployments.
NOTES:
- For local or Cloud Run deployments, there are little/no updates to go from Python 2 to 3.
- Neither Cloud Functions nor Cloud Run with Cloud Buildpacks support Python 2.
| File | Description |
|---|---|
main.py |
main application file |
templates/index.html |
application HTML template |
requirements.txt |
3rd-party package requirements file |
app.yaml |
App Engine configuration file (only for App Engine deployments) |
.gcloudignore |
files to exclude deploying to the cloud (administrative) |
noxfile.py |
unit tests nox tool setup file |
test_sheets.py |
unit tests (pytest) |
Procfile |
"Entrypoint" directive Procfile to start app (only for Cloud Run deployments using Cloud Buildpacks) |
README.md |
this file (administrative) |
Below are the required settings and instructions for all documented deployments. The "TL:DR;" section at the top of each configuration summarizes the key files (see above) while the table beneath spells out the details. No administrative files are listed.
Local Flask server (Python 2 or 3)
TL;DR: application files (main.py & requirements.txt)
- Login using Application Default Credentials: run the
gcloud auth application-default logincommand and ensure you do not set theGOOGLE_APPLICATION_CREDENTIALSenvironment variable; also see the command reference. - (2.x only) Uncomment/enable the line for
google-authinrequirements.txt - Run
pip install -U pip -r requirements.txtto install/update packages locally (orpip2for Python 2 orpip3for Python 3 explicitly) - Run
python main.pyto run on local Flask server (python2orpython3to be explicit)
Troubleshooting: if you're on a VM running pip install, you may get an error like this:
ERROR: Could not install packages due to an OSError: [Errno 13] Permission denied: 'PKG-INFO'
Consider using the `--user` option or check the permissions.
Solution: If this is the case:
- Update the line for
flaskinrequirements.txtto:flask<2.0dev
TL;DR: app files plus app.yaml
- Run
gcloud app deploy
TL;DR: app files plus app.yaml, appengine_config.py, and lib
- Copy the Cloud API Python 2
app.yamlfile - Copy the Cloud API Python 2
appengine_config.pyfile - Uncomment/enable the line for
google-authinrequirements.txt - Uncomment "<1.12.0" on the line for
google-api-python-clientinrequirements.txt - Run
pip install -t lib -r requirements.txtto populatelibfolder (orpip2) - Run
gcloud app deploy
TL;DR: app files
- Run
gcloud functions deploy students --runtime python39 --trigger-http --allow-unauthenticatedto deploy to Cloud Functions (or Python 3.7 or 3.8)
- Cloud Functions does not support Python 2.
- The Cloud Function name must match the function's name in
main.pyelse you need to use--entry-point.
TL;DR: app files plus Procfile
- Run
gcloud run deploy students --allow-unauthenticated --platform managedto deploy to Cloud Run; optionally add--source . --region REGIONfor non-interactive deploy
- There is no support for Python 2 with Cloud Buildpacks (2.x developers must use Docker)
NOTE: This sample uses the Flask development server by default for prototyping; for production, bundle and deploy a production server like
gunicorn:
- Uncomment
gunicornfromrequirements.txt(commented out for App Engine & Cloud Functions)- Uncomment
ENTRYPOINTentry forgunicorninDockerfilereplacing default entry- Re-use the same deploy command above
TL;DR: app files plus Dockerfile (nearly identical to Python 2 deployment)
- Copy the Cloud API Python
Dockerfilefile - Edit
Dockerfileand switch theFROMentry to thepython:3-slimbase image - Run
gcloud run deploy students --allow-unauthenticated --platform managedto deploy to Cloud Run; optionally add--source . --region REGIONfor non-interactive deploy
- The
gunicornsidebar above also applies here.
TL;DR: app files plus Dockerfile
- Copy the Cloud API Python
Dockerfilefile - Run
gcloud run deploy students --allow-unauthenticated --platform managedto deploy to Cloud Run; optionally add--source . --region REGIONfor non-interactive deploy
- The
gunicornsidebar above also applies here.
These are relevant links only to the app in this folder (for all others, see the README one level up:
- Google Sheets API home page
- Google Sheets API Python QuickStart (origin of the student spreadsheet)
- Google Sheets API intro codelab (Node.js)
- Google Sheets API videos (Python or Apps Script [JavaScript])
- Google Workspace APIs home page
- Google APIs client library for Python
- Google Cloud serverless home page
- Cloud Functions home page
- Cloud Functions Python quickstart
- Cloud Run home page
- Cloud Run Python quickstart
- App Engine home page
- App Engine Python 3 quickstart
- App Engine (standard environment) Python 3 runtime
- App Engine (standard environment) Python 2 runtime
- Differences between Python 2 & 3 App Engine (standard environment) runtimes
- App Engine (standard environment) Python 2 to 3 migration guide
- App Engine (standard environment) to Cloud Run codelab tutorial (via Docker)
- App Engine (standard environment) to Cloud Run codelab tutorial (no Docker/
Dockerfilevia Cloud Buildpacks)
Testing is driven by nox which uses pytest for testing and flake8 for linting, installing both in virtual environments along with application dependencies, flask and google-api-python-client, and finally, blinker, a signaling framework integrated into Flask. To run the lint and unit tests (testing GET and POST requests), install nox (with the expected pip install -U nox) and run it from the command line in the application folder and ensuring noxfile.py is present.
$ nox
nox > Running session tests-2.7
nox > Creating virtual environment (virtualenv) using python2.7 in .nox/tests-2-7
nox > python -m pip install pytest blinker flask google-auth google-api-python-client
nox > pytest
================================================== test session starts ==================================================
platform darwin -- Python 2.7.16, pytest-4.6.11, py-1.11.0, pluggy-0.13.1
rootdir: /tmp/noncloud/python/sheets
collected 1 item
test_sheets.py . [100%]
=============================================== 1 passed in 1.84 seconds ================================================
nox > Session tests-2.7 was successful.
nox > Running session tests-3.8
nox > Creating virtual environment (virtualenv) using python3.8 in .nox/tests-3-8
nox > python -m pip install pytest blinker flask google-auth google-api-python-client
nox > pytest
================================================== test session starts ==================================================
platform darwin -- Python 3.8.2, pytest-6.2.5, py-1.11.0, pluggy-1.0.0
rootdir: /tmp/noncloud/python/sheets
collected 1 item
test_sheets.py . [100%]
=================================================== 1 passed in 1.04s ===================================================
nox > Session tests-3.8 was successful.
nox > Running session tests-3.9
nox > Creating virtual environment (virtualenv) using python3.9 in .nox/tests-3-9
nox > python -m pip install pytest blinker flask google-auth google-api-python-client
nox > pytest
================================================== test session starts ==================================================
platform darwin -- Python 3.9.1, pytest-6.2.5, py-1.11.0, pluggy-1.0.0
rootdir: /tmp/noncloud/python/sheets
collected 1 item
test_sheets.py . [100%]
=================================================== 1 passed in 0.97s ===================================================
nox > Session tests-3.9 was successful.
nox > Running session lint-2.7
nox > Creating virtual environment (virtualenv) using python2.7 in .nox/lint-2-7
nox > python -m pip install flake8
nox > flake8 --show-source --builtin=gettext --max-complexity=20 --exclude=.nox,.cache,env,lib,generated_pb2,*_pb2.py,*_pb2_grpc.py --ignore=E121,E123,E126,E203,E226,E24,E266,E501,E704,W503,W504,I202 --max-line-length=88 .
nox > Session lint-2.7 was successful.
nox > Running session lint-3.8
nox > Creating virtual environment (virtualenv) using python3.8 in .nox/lint-3-8
nox > python -m pip install flake8
nox > flake8 --show-source --builtin=gettext --max-complexity=20 --exclude=.nox,.cache,env,lib,generated_pb2,*_pb2.py,*_pb2_grpc.py --ignore=E121,E123,E126,E203,E226,E24,E266,E501,E704,W503,W504,I202 --max-line-length=88 .
nox > Session lint-3.8 was successful.
nox > Running session lint-3.9
nox > Creating virtual environment (virtualenv) using python3.9 in .nox/lint-3-9
nox > python -m pip install flake8
nox > flake8 --show-source --builtin=gettext --max-complexity=20 --exclude=.nox,.cache,env,lib,generated_pb2,*_pb2.py,*_pb2_grpc.py --ignore=E121,E123,E126,E203,E226,E24,E266,E501,E704,W503,W504,I202 --max-line-length=88 .
nox > Session lint-3.9 was successful.
nox > Ran multiple sessions:
nox > * tests-2.7: success
nox > * tests-3.8: success
nox > * tests-3.9: success
nox > * lint-2.7: success
nox > * lint-3.8: success
nox > * lint-3.9: success