Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to use Django 1.6 with appengine dev_appserver.py + not supported third library

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.

like image 211
samidarko Avatar asked Jan 08 '14 09:01

samidarko


2 Answers

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.

like image 159
casjorge Avatar answered Oct 10 '22 15:10

casjorge


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.

like image 27
Daniel Roseman Avatar answered Oct 10 '22 15:10

Daniel Roseman