5

I'm trying to set a local environment for a Django app that will be hosted on GAE

  • My first problem: I can't ask Django 1.6 in app.yaml (not supported) so I switched to "latest" version but I'm not sure of what I will get by doing this ?

  • My second one: I want to use some extra library such as "django-taggit". I created a "libs" directory in my project where I copied the "taggit" directory. I added the absolute path to PYTHONPATH first then I moved it to PATH then also tried in wsgi.py with sys.path.append but I keep having an import error (ImportError: No module named taggit) when I'm running dev_appserver.py ?

Is it me or there's a lack of documentation about what I want to do ? I spent a lot of time googling without clear results. Everything seems trivial when you follow the google way with webapp2 but much more complicate when you don't. Is GAE a right choice for Django hosting ?

Thanks by advance for your help and advice.

samidarko
  • 590
  • 7
  • 20
  • I've been running a Django app on GAE with Google Cloud SQL as db for 9 months now and find it running pretty smoothly. – Roger Jan 08 '14 at 16:12
  • Roger can you please answer this quesion http://stackoverflow.com/questions/21002638/django-on-google-app-engine-with-cloud-sql-in-dev-environment – Sandeep Jan 09 '14 at 08:53

4 Answers4

6

It is possible to use any library you want in Google App Engine in general, however it is necessary to take into account that GAE has some considerations (i.e. GAE does not manage file structures in the same way usually some portions of code use to do, in this case it is necessary to use boto and google cloud storage and other stuff).

Currently GAE has not Django 1.6 to use as builtin library (third party) but GAE permits to include your own django version with no limits. You need in this last case to modify sys.path and app.yaml. To modify in production sys.path, you can use this code in wsgi.py in example:

#wsgi.py ' Locate in main folder
PROJECT_DIR = os.path.abspath(os.path.dirname(__file__))
PACKAGES_DIR = os.path.join(PROJECT_DIR, 'Lib', 'site-packages')

def add_dir_to_path(dir):
    if dir not in sys.path or sys.path.index(dir) > 0:
        while dir in sys.path:
            sys.path.remove(dir)
        sys.path.insert(0, dir)

add_dir_to_path(PROJECT_DIR)
add_dir_to_path(PACKAGES_DIR)

#Delete current django version in production
for key in [key for key in sys.modules if key.startswith('django')]:
  del sys.modules[key]

os.environ["DJANGO_SETTINGS_MODULE"] = 'myapp.settings'
from google.appengine.ext.webapp import util
# Force Django to reload its settings.
from django.conf import settings
settings._target = None

And in app.yaml you can use:

application: myappinGAE
version: 1
runtime: python27
api_version: 1
threadsafe: true

env_variables:
    DJANGO_SETTINGS_MODULE: 'myapp.settings'

handlers:
- url: /.*
  script: wsgi.application

This changes will say to GAE to use your own django version which lives in /Lib/site-packages/django. However, I am having some other troubles with django 1.6 (database authentication is not working), but to init, this code will help you, and database authentication works well for Django 1.4 or Django 1.5.

casjorge
  • 241
  • 2
  • 5
  • Is there any downside in not using the "supported" Django from Google, but rather using the workout around outlined here? Given that google now offers a SQL backend, I am not concerned about having django-nonrel. – Alex Rothberg Aug 01 '14 at 21:40
3

On the contrary, the documentation is quite clear about this. Any third-party libraries that are not supplied by the SDK need to be installed within your app directory.

The dev server starts its own sandbox environment and ignores your existing PYTHONPATH, and you can't change sys.path in the production environment anyway.

Daniel Roseman
  • 588,541
  • 66
  • 880
  • 895
  • Thanks for saving my time. I read this but when the doc said "installed within your app directory" I thought must be inside and not necessarily at the root level which is not very "clean" if you start to have a few. So it's not possible to move them somewhere else ? – samidarko Jan 08 '14 at 12:09
  • Depends how you think about it. It's pretty clean since it separates everything that's being uploaded to your production server from the rest of your development system. You can put all the libraries within a separate libs folder within your app folder. See this answer: http://stackoverflow.com/questions/18248784/how-can-i-simply-modify-the-path-for-the-entire-requests-package-within-a-librar – dragonx Jan 08 '14 at 15:35
3

I believe the correct way to change your path is using an appengine_config.py file because it runs first in your application. More info.

Now here's the structure I use for my appengine apps.

- /lib                 # This is where all my 3rd party libs go
- /myApp               # This is where my django app goes
- /static              # Static Files
- appengine_config.py  # Initial configuration
- app.yaml

Now on my appengine_config.py

import os
import sys
sys.path.insert(0,os.path.join(os.path.dirname(__file__),  'lib' ))
sys.path.insert(0,os.path.join(os.path.dirname(__file__), 'myApp'))

And just for the sake of it here is my app.yaml

application: myApp
version: 1
runtime: python27
api_version: 1
threadsafe: yes

env_variables:
  DJANGO_SETTINGS_MODULE: 'myApp.settings'

handlers:
- url: /static
  static_dir: static

builtins:
- django_wsgi: on

This structure has been working very well for me.

But most often I use Django 1.4 and 1.5. I don't have Apps with Django 1.6 so I never have to include django itself, I can use the available library instead.

Eduardo
  • 22,574
  • 11
  • 76
  • 94
0

Adding Djangononrel is also possible, but take into consideration that djangononrel is evolving and is a fork of official django. It is necesary to check if libraries you are going to use in your django application are compatible with this fork.

casjorge
  • 241
  • 2
  • 5