In this tutorial, we will show you how to use Eclipse to create a Google App Engine (GAE) Python web project (hello world example), run it locally, and deploy it to Google App Engine account.

Tools used :

  1. Python 2.7
  2. Eclipse 3.7 + PyDev plugin
  3. Google App Engine SDK for Python 1.6.4

P.S Assume Python 2.7 and Eclipse 3.7 are installed.

1. Install PyDev plugin for Eclipse

Use following URL to install PyDev as Eclipse plugin.

http://pydev.org/updates

Figure 1 – In Eclipse , menu, “Help –> Install New Software..” and put above URL. Select “PyDev for Eclipse” option, follow steps, and restart Eclipse once completed.

pydev eclipse

2. Verify PyDev

After Eclipse is restarted, make sure PyDev’s interpreter is pointed to your “python.exe“.

Figure 2 – Eclipse -> Windows –> Preferences, make sure “Interpreter – Python” is configured properly.

pydev eclipse config

3. Google App Engine SDK Python

Download and install Google App Engine SDK for Python.

4. Python Hello World in Eclipse

Following steps to show you how to create a GAE project via Pydev plugin.

Figure 4.1 – Eclipse menu, File -> New -> Other… , PyDev folder, choose “PyDev Google App Engine Project“.

gae python hello world example

Figure 4.2 – Type project name, if the interpreter is not configure yet (in step 2), you can do it now. And select this option – “Create ‘src’ folder and add it to PYTHONPATH“.

gae python hello world example

Figure 4.3 – Click “Browse” button and point it to the Google App Engine installed directory (in step 3).

gae python hello world example

Figure 4.4 – Name your application id in GAE, type anything, you can change it later. And choose “Hello Webapp World” template to generate the sample files.

gae python hello world example

Figure 4.5 – Done, 4 files are generated, Both “.pydevproject” and “.project” are Eclipse project files, ignore it.

gae python hello world example

Review the generated Python’s files :

File : helloworld.py – Just output a hello world.

from google.appengine.ext import webapp
from google.appengine.ext.webapp.util import run_wsgi_app
 
class MainPage(webapp.RequestHandler):
 
    def get(self):
        self.response.headers['Content-Type'] = 'text/plain'
        self.response.out.write('Hello, webapp World!')
 
application = webapp.WSGIApplication([('/', MainPage)], debug=True)
 
def main():
    run_wsgi_app(application)
 
if __name__ == "__main__":
    main()

File : app.yaml – GAE need this file to run and deploy your Python project, it’s quite self-explanatory, for detail syntax and configuration, visit yaml and app.yaml reference.

application: mkyong-python
version: 1
runtime: python
api_version: 1
 
handlers:
- url: /.*
  script: helloworld.py

5. Run it locally

To run it locally, right click on the helloworld.py, choose “Run As” –> “Run Configuration”, create a new “PyDev Google App Run“.

Figure 5.1 – In Main tab -> Main module, manually type the directory path of “dev_appserver.py“. “Browse” button is not able to help you, type manually.

gea python run locally

Figure 5.2 – In Arguments tab -> Program arguments, put “${project_loc}/src“.

gea python run locally

Figure 5.3 – Run it. By default, it will deploy to http://localhost:8080.

gea python run locally

Figure 5.4 – Done.

gea python run locally

5. Deploy to Google App Engine

Register an account on https://appengine.google.com/, and create an application ID for your web application. Review “app.yaml” again, this web app will be deployed to GAE with application ID “mkyong-python“.

File : app.yaml

application: mkyong-python
version: 1
runtime: python
api_version: 1
 
handlers:
- url: /.*
  script: helloworld.py

To deploy to GAE, see following steps :

Figure 5.1 – Create another new “PyDev Google App Run”, In Main tab -> Main module, manually type the directory path of “appcfg.py“.

deploy python to GAE

Figure 5.2 – In Arguments tab -> Program arguments, put “update ${project_loc}/src“.

deploy python to GAE

Figure 5.3 – During deploying process, you need to type your GAE email and password for authentication.

deploy python to GAE

Figure 5.4 – If success, the web app will be deployed to – http://mkyong-python.appspot.com/.

deploy python to GAE

Done.

References

  1. PyDev Plugin for Eclipse
  2. Yaml Official Website
  3. GAE getting start with Python
  4. Install PyDev for Eclipse
  5. GAE Java hello world example using Eclipse
Tags :
Founder of Mkyong.com, love Java and open source stuff. Follow him on Twitter, or befriend him on Facebook or Google Plus. If you like my tutorials, consider making a donation to the charity, thanks.

Related Posts

Popular Posts