The Google Cloud SDK and App Engine SDK for Python each include a local development server that you can run locally to simulate your application running in production App Engine. The simulated environment enforces some sandbox restrictions, such as restricted system functions and Python module imports, but not others, like request time-outs or quotas.
The local development server also simulates the services provided by App Engine Python SDK libraries, including Datastore, Memcache, and Task Queues, by performing their tasks locally. Note that when your application is running in the development server, you can still make remote API calls to the production infrastructure using Google APIs HTTP endpoints.
Running the local development server
Once you have a directory for your application and an
app.yaml configuration file,
you can start the local development server using the dev_appserver.py command.
Running the local development server (dev_appserver.py)
To run the local development server, you can either run dev_appserver.py by
specifying the full directory path or you can add dev_appserver.py to your
PATH environment variable:
-
If you installed the App Engine SDK, the tool is located at
[PATH_TO_APP_ENGINE_SDK]/dev_appserver.py. -
If you installed the Google Cloud SDK, the tool is located at
[PATH_TO_CLOUD_SDK]/google-cloud-sdk/bin/dev_appserver.py.Tip: To add the Google Cloud SDK tools to your PATH and enable command-completion in your bash shell, you can run:
[PATH_TO_CLOUD_SDK]/google-cloud-sdk/install.sh
dev_appserver.py [PATH_TO_YOUR_APP]
The local server listens on port 8080 by default. You can visit the application at this URL: http://localhost:8080/.
To change which port the local server uses, use the --port option:
dev_appserver.py --port=9999 [PATH_TO_YOUR_APP]
To stop the local server: with Mac OS X or Unix, press Control-C or with Windows, press Control-Break in your command prompt window.
Specifying application IDs
If you want to use application IDs with the local development server, don't use
the App ID from the APPLICATION_ID environment variable. The development
server differs from production App Engine service in that the local server
prepends the string dev~ to the APPLICATION_ID environment variable.
Instead, if you need to access your App ID in the local server, for example to
spoof an email address, use the get_application_id() function. To get the hostname of the running app, use the get_default_version_hostname() function.
Detecting application runtime environment
To find out whether your code is running in production or in the local
development server, check
if os.getenv('SERVER_SOFTWARE', '').startswith('Google App Engine/'). When
this is True, you're running in production; otherwise, you're running in the
local development server.
Using the local Datastore
The local development server simulates the App Engine datastore using a local file that persists between invocations of the local server.
For more information on indexes and index.yaml, see the
Datastore Indexes and Datastore
Index Configuration pages.
Browsing the local Datastore
If your app has written data to your local Datastore using the local development server, you can browse it in the local development console.
To browse local Datastore:
-
Access the Datastore Viewer in the local development console. (The URL is
http://localhost:8000/datastore.) -
View your local Datastore contents.
Specifying the ID allocation policy
For production App Engine, you can set the Datastore to automatically generate entity IDs.
Although the auto ID assignment policies for the production server are completely different than those used by the development server, you can also set the automatic ID allocation policy for the local server.
To specify the automatic ID assignment policy, use the --auto_id_policy
option:
dev_appserver.py --auto_id_policy=sequential
where --auto_id_policy can be one of the following:
scattered: (default) IDs are assigned from a non-repeating sequence of approximately uniformly distributed integers.sequential: IDs are assigned from the sequence of consecutive integers.
Clearing the local Datastore
To clear the local datastore for an application, invoke the local development server as follows:
dev_appserver.py --clear_datastore=yes [PATH_TO_YOUR_APP]
Changing local Datastore location
To change the location used for the datastore file, use the --datastore_path
option:
dev_appserver.py --datastore_path=/tmp/myapp_datastore [PATH_TO_YOUR_APP]
Using the Users service
App Engine provides a Users Service to
simplify authentication and authorization for your application. The local
development server simulates the behavior of Google Accounts
with its own sign-in and sign-out pages. While running under the local
development server, the
users.create_login_url
and users.create_logout_url
functions return URLs for /_ah/login and /_ah/logout on the local server.
Using Mail
The local development server can send email for calls to the App Engine mail service using either an SMTP server or a local installation of Sendmail.
Using SMTP
To enable mail support with an SMTP server, invoke dev_appserver.py as
follows::
dev_appserver.py --smtp_host=smtp.example.com --smtp_port=25 \
--smtp_user=ajohnson --smtp_password=k1tt3ns [PATH_TO_YOUR_APP]
where you set the --smtp_host, --smtp_port, --smtp_user and
--smtp_password options with your own values.
Using Sendmail
To enable mail support with Sendmail, invoke dev_appserver.py as follows:
dev_appserver.py --enable_sendmail=yes
The local server will use the sendmail command to send email messages with
your installation's default configuration.
Using URL Fetch
When your application uses the URL fetch API to make an HTTP request, the local development server makes the request directly from your computer. The URL Fetch behavior on the local server may differ from production App Engine if you use a proxy server for accessing websites.
Using the Interactive Console
The Interactive Console allows developers to enter arbitrary Python code into a web form and execute it inside their app's environment; it provides the same access to the application's environment and services as a .py file inside the application itself.
To use the Interactive Console:
-
Access the Interactive console in the in the local development console. (The URL is
http://localhost:8000/console.) -
Enter any Python code you'd like to run in the text area, then submit the form to execute it. For example the following code will add a Datastore entity called
Greetingwith text content ofHello:from google.appengine.ext import ndb class Greeting(ndb.Model): content = ndb.TextProperty() e = Greeting(content="Hello") e.put()
Debugging with PDB
To use the Python PDB debugger:
-
Add this line into your code:
import pdb; pdb.set_trace();dev_appserverwill break at this point, and drop into the PDB REPL, allowing you to debug your code from the command line. -
If your application makes multiple simultaneous requests that invoke
pdb.set_trace(), multiple debugging sessions will start concurrently, each of which sends output to STDOUT. To avoid this, serialize your requests by disabling thedev_appservermulti-threading and multi-processing support, as follows:-
Disable multi-threading for
- All modules using the
--threadsafe_override=falseflag. - One module using the
--threadsafe_override=<MODULENAME>:falseflag - Multiple modules using the
--threadsafe_override=<MODULE1NAME>:false,<MODULE2NAME>:falseflag
- All modules using the
-
Disable multi-processing for
- All modules using the
--max_module_instances=1flag - One module using the
--max_module_instances=<MODULENAME>:1flag; - Multiple modules using the
--max_module_instances=<MODULE1NAME>:1,<MODULE2NAME>:1flag
- All modules using the
-