views:

66

answers:

2

Hi,

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

+2  A: 

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.

Max
I need some django models, so I can't eliminate django code.I read the first point and the google documentation, but it doesn't really help. Can you tell me more?
Better answered by another active AEP developer. You should look at the AEP code to see what hooks it offers to set up the environment. Some ideas to try:`from common.appenginepatch.aecmd import setup_env``setup_env(manage_py_env=True)`Otherwise, you might try:`from common.appenginepatch.main import *`which (I think) will load the full django stack.
Max
A: 

Why not:

sys.path.append('common/appenginepatch')

since the ragendja is in this directory?

Pawel Markowski
I tried it, but it doesn't work. I don't know why