Report Bugs/Request Features/Provide Feedback via GitHub or Email
Try our GUI for GAE - NoCommandLine
Try our Analytics App for GAE & Cloud Run Apps - NoCommandLine (Analytics)
This is a patch to allow you run Python 3 Apps on Windows using dev_appserver.py
(ONLY for GAE Standard Environment).
Tested on:
-
Windows 10 Home Edition, Google Cloud SDK 470.0.0, app-engine-python 1.9.111 🆕
Note: Google Cloud SDK 470.0.0 requires a minimum of Python 3.9
-
Windows 10 Home Edition, Google Cloud SDK 429.0.0, app-engine-python 1.9.103, app-engine-python-extras 1.9.96
-
Windows 10 Home Edition, Google Cloud SDK 407.0.0, app-engine-python 1.9.101, app-engine-python-extras 1.9.96
-
Windows 10 Home Edition, Google Cloud SDK 391.0.0, app-engine-python 1.9.100, app-engine-python-extras 1.9.96
According to Google
The dev_appserver tool does not support development of Python 3 apps on Windows.
A high level summary of the changes/code in the patch
-
If your App doesn't include an 'entrypoint',
dev_appserver.py
will add the default entrypoint,gunicorn -b :${PORT} main:app
. This meansdev_appserver.py
runs your App withgunicorn
because this is what Google uses in Production.Since
gunicorn
doesn't run on Windows, the Patch replaces it with another WSGI server,waitress
, when it detects you're running Windows and uses the default entrypoint,waitress-serve --listen=*:${PORT} main:app
. Note that when your App is deployed to production, it will still be run withgunicorn
. -
Windows uses theThis has been fixed by GoogleScript
folder instead ofbin
folder for storing python executables.dev_appserver.py
includedbin
folder in the paths to executable files. The Patch usesScript
folder when it detects you're running Windows. -
dev_appserver.py
first creates a copy of your requirements file via the commandtempfile.NamedTemporaryFile()
, addsgunicorn
to the bottom of the copy and then sends this copy to a function which re-opens the file, reads its contents and installs the requirements.However, Windows doesn't allow reopening of a temporary file via its filename while the file is still open (reference)
The Patch solves this problem by not creating a copy of the requirements file. Instead, it installs the contents of the original requirements file, after which it then installs
waitress
. -
The Patch added the environment variable
PIP_USER
and set it toFalse
because callingpip -m install <package_name>
on Windows viasubprocess.Popen()
can sometimes lead to the error'[WinError 5] Access is denied: Consider using the --user option or check the permissions'.
If you then run
pip -m --user install <package_name>
, you get another error -Can not perform a '--user' install. User site-packages are not visible in this virtualenv.
Setting
PIP_USER = False
solves all of the above error i.e. it allows you to runpip -m install <package_name>
(reference).
-
instance_factory.py
Location:
<SDK_INSTALL_PATH>\Cloud SDK\google-cloud-sdk\platform\google_appengine\google\appengine\tools\devappserver2\python\
-
http_runtime.py
Location:
<SDK_INSTALL_PATH>\Cloud SDK\google-cloud-sdk\platform\google_appengine\google\appengine\tools\devappserver2\
Note:
- SDK_INSTALL_PATH = The path to Google Cloud SDK/CLI installation on your machine
- If you don't see the path/directory
google_appengine\google\appengine\tools\devappserver2\python\
, it probaly means you don't haveapp-engine-python-extras
installed. Run the commandgcloud components install app-engine-python-extras
and it should create the missing path/directories
In the src
folder, pick the folder which matches your Google Cloud SDK version
- For Google Cloud SDK Version 470.0.0 and above, choose
gcloud_sdk_70.0.0+
- For Google Cloud SDK Version 427.0.0 and above, choose
gcloud_sdk_427.0.0+
(Google Cloud SDK Version 427.0.0 introduced a breaking change) - For Google Cloud SDK Version below 427.0.0, choose
gcloud_sdk_426.0.0-
For each of the files listed under 'Changed Files',
-
Navigate to the location
-
Create a backup of the file
-
Download our copy of the file from the 'src' folder and overwrite the original file
When done, run your app with the dev_appserver.py
command as usual i.e.
dev_appserver.py --runtime_python_path=<PYTHON3_PATH> --application=<PROJECT_ID> app.yaml --port=<PORT_NO>
Note:
- Don't include your Python2 Path in the values for the flag
--runtime_python_path
- For
Cloud SDK 427.0.0 and above
, don't forget to set the environment variableCLOUDSDK_DEVAPPSERVER_PYTHON
to the path of your Python 2 interpreter. If you don't, you'll get an error when trying to run your App withdev_appserver.py
. For more details, see Google documentation
-
Flag to reuse existing virtual environment:
This is now officially supported in gcloud CLI version 422.0.0 via the
--python_virtualenv_path
flagEach time you rundev_appserver.py
, it creates & activates a new virtual environment (in your temp folder) and installs your requirements.txt file. This can slow down your application startup (even if it's installing the libraries from cache). In addition,dev_appserver.py
doesn't delete the previously created temp folders. This means that over time (especially when you're debugging which leads to multiple app restarts), your temp folder becomes littered with temp virtual environmentsThe plan is to create a flag to telldev_appserver.py
to use an existing virtual environment like thevenv
folder which you would typically create in your project root. -
Flag to delete the virtual environment created in temp when the application shuts down
If you found this work useful, please support us with a tip/gift - Give a tip