5/12/2011: Updated to support Google App Engine Version 1.5.0.
A while back I put together the information and steps to get Google App Engine running on Windows wrapped up in a portable little bundle. Since it has been a while I thought it could use a good refresh with the latest versions of everything and make things work more like the real Google App Engine. Some of the content may seem the same as last time but there definitely will be some new ways of running things and new capabilities that were not available last time. This new way is much easier and should keep up with all the changes that newer versions of Google App Engine may bring.
Let’s gather all the files that we are going to need. I throw the version numbers on here that this was written for but yours may vary slightly. Download the following:
- Portable Python (Download the 2.5.x release. I am using 2.5.4 at the moment.)
- Google App Engine SDK (Download the Linux/Other Platforms release. I am using 1.4.1-2011-01-05 at the moment.)
Now that you’ve got all the files, you can grab a USB thumb drive or even just install it to a directory on your local machine. Run the Portable Python installer and specify the location you’d like to store it. I will refer to this location as the “Portable Python folder” from here on.
Extract the “google_appengine” folder from the Google App Engine SDK zip file into the Portable Python folder on your thumb drive. Side Note: In the future if you ever want to update to a newer release, just delete the “google_appengine” folder and extract a new copy from the latest zip file from Google.
Create a new folder in your Portable Python folder called “google_appengine_datastores”. This is where we are going to store all the data for our apps.
Create two new files called “dev_appserver.py.bat” and “appcfg.py.bat” in your Portable Python folder both with the following contents:
1 2 3 4 5 6 7 8
@echo off REM Portable Google App Engine Command Line Rewriter REM Written by David Lambert (http://www.codepenguin.com) REM Execute the command line rewriting script app\python.exe _cmdrewrite.py %0 %* REM Execute the new command line batch file _cmdrewrite.bat
Create a new file called “_cmdrewrite.py” in your Portable Python folder with the following contents:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20
#Portable Google App Engine Command Line Rewriter #Written by David Lambert (http://www.codepenguin.com) #Rewrite the command line with correct paths import sys, os, re new_cmd = 'app\python.exe google_appengine/%s' % sys.argv gae_version = re.search('release: "(.*?)"',open('google_appengine/VERSION','r').read()).group(1) appname = sys.argv[-1].rstrip('/').split('/')[-1] if sys.argv == 'dev_appserver.py' and os.path.exists(appname): new_cmd += ' --blobstore_path=google_appengine_datastores/%s_blobstore' % (appname) new_cmd += ' --datastore_path=google_appengine_datastores/%s_datastore' % (appname) new_cmd += ' --history_path=google_appengine_datastores/%s_history' % (appname) if gae_version >= "1.5.0": new_cmd += ' --skip_sdk_update_check' new_cmd += ' --rdbms_sqlite_path=google_appengine_datastores/%s_rdbms' % (appname) new_cmd += " " + " ".join(sys.argv[2:]) #Write new command to the batch file f = open('_cmdrewrite.bat','w') f.write(new_cmd) f.close()
The above three files make up the command line rewriting system. It basically will take a normal command line that you see on the Google App Engine tutorials and documentation and rewrite them to work with Portable Python. This makes the Portable Google App Engine system much more useful because you can easily follow along with the real documentation and tutorials without having to adjust or rewrite your own command lines. It also makes the datastore and history files portable by storing them in the “google_appengine_datastore” folder created earlier.
Creating your Application
Now that we’ve done all the prep work let’s see how this thing actually works. Creating a new application does take a few steps but they are pretty basic and boring. The quick and dirty steps are as follows:
- Create the App folder.
- Create a bunch of source files in your App folder.
- Run your App!
I’ll walk you through all the above steps to give you an idea of what is going on. We are going to use the example source from Google for the actual example app content parts. It is a great idea to read over their documentation as it will give you more insight into how the Google App Engine works.
All of our applications are going to reside in their own folders inside the Portable Python folder. Create a new folder called “helloworld” in your Portable Python folder. I’ll refer to this as the App folder from now on as all the important files will go inside this folder for our first app. The next few steps come directly from the Hello World tutorial from Google.
Create a new file called “helloworld.py” in your App folder with the following contents:
1 2 3
print 'Content-Type: text/plain' print '' print 'Hello, world!'
Create a new file called “app.yaml” in your App folder with the following contents:
1 2 3 4 5 6 7 8
application: helloworld version: 1 runtime: python api_version: 1 handlers: - url: /.* script: helloworld.py
The super important file here is the “app.yaml”. You can think of it as the master control center of your App. It has all the settings and such that tell Google App Engine what to do. Google’s documentation lays out everything for this file. We’ve got enough in ours above to get going but I definitely recommend reading their docs when you get a chance.
Running your Application
Our HelloWorld App is ready to go! Let’s get this thing up and running. We are going to need a new command interpreter window opened to our Portable Python folder. Now type the following:
After pressing enter you should see something close to the following:
P:\PortablePython_1.1_py2.5.4>dev_appserver.py helloworld INFO 2011-01-08 15:21:00,736 appengine_rpc.py:153] Server: appengine.google.com WARNING 2011-01-08 15:21:00,743 datastore_file_stub.py:573] Could not read data store data from P:\PortablePython_1.1_py2.5.4\google_appengine_datastores\helloworld_datastore INFO 2011-01-08 15:21:01,075 dev_appserver_main.py:485] Running application helloworld on port 8080: http://localhost:8080
You can ignore the warning in the middle that says “Could not read data store data”. That just means that the datastore has not been created yet. Now open a web browser and point it to the following URL: http://localhost:8080. You should now see “Hello, world!” in your browser! That is it! Your first Google App Engine application is running. When you want to stop your application press Ctrl+Pause/Break. It will prompt you “Terminate batch job (Y/N)?” so just press ‘y’ and then press enter.
Other available commands
Google App Engine’s dev_appserver.py has a lot of options and functionality to help you manage your application. The new command rewriting system should be compatible with all the options that are available. You should definitely read through the options in their documentation so that you can utilize things like clearing the datastore, changing the webserver port, using SQLite for the datastore and much more. Let’s look at a few of these commands that you will see in the tutorials.
To run the development app server and clear the current datastore use the following command:
dev_appserver.py --clear_datastore helloworld
To run the development app server using port 9999 use the following command:
dev_appserver.py --port=9999 helloworld
If you want to use SQLite for the datastore (increased performance for large amounts of content), use the following command:
dev_appserver.py --use_sqlite helloworld
You also can use the appcfg.py commands to upload your application by using the following command:
appcfg.py update helloworld
You now have a fully functioning Portable Google App Engine development environment! You can follow all the tutorials on the Google App Engine website with ease. If you ever need to update the Google App Engine SDK, just delete the google_appengine folder from your Portable Python folder and extract the new one from the main download site. If you have any questions or ideas on how to improve this system, please let me know!