Search code examples
pythondjangogoogle-app-engineapp-engine-patchdjango-settings

Sys.path modification or more complex issue?


I have problems with importing correctly a module on appengine. My app generally uses django with app-engine-patch, but this part is task queues using only the webapp framework.

I need to import django settings for the app to work properly.

My script starts with:

import os
import sys
sys.path.append('common/')
# Force Django to reload its settings.
from django.conf import settings
settings._target = None

# Must set this env var before importing any part of Django
os.environ['DJANGO_SETTINGS_MODULE'] = 'settings' 

I always get this error, or something related:

<type 'exceptions.ImportError'>: No module named ragendja.settings_pre

because the settings.py file starts with

from ragendja.settings_pre import *

I think I need to add ragendja to sys.path again but I had several tries that didn't work.

Here is my directory:

project/
    app.yaml
    setting.py
    common/
        appenginepatch/
            ragendja/
                setting_pre.py
    myapp/
        script.py 

Is it only a sys.path problem and how do I need to modify it with the correct syntax?

Thanks


Solution

  • App engine patch manipulates sys.path internally. Background tasks bypass that code, so your path will not be ready for Django calls. You have two choices:

    • Fix the paths manually. The app engine documentation (see the sub-section called "Handling import path manipulation") suggests factoring the path manipulation code into a module that can be imported by your task script.

    • Eliminate dependencies on django code, if possible. If you can write your task to be pure python and/or google api calls, you're good to go. In your case, this might mean refactoring your settings code.