I'm struggling with imports on Google App Engine. I have a Django/Python project with the following (standard) setup:
[myproject]
|
-[myproject]
|
--[app1]
|
--[app2]
The second [myproject]
directory is (I think) considered the project root and is named the same as the top level directory of the project. This structure was created by Django's startproject
command.
Say, I have a class MyClass
in app1.models
which I want to use in app2.models
. When I develop locally using the GAE SDK, I can do:
from app1.models import MyClass
However, when I deploy to Google App Engine, I need to rephrase that import to:
from myproject.app.models import MyClass
Otherwise GAE will not find the class and abort with an error. Also, in my settings.py
I can locally refer to INSTALLED_APPS
as:
'app1',
'app2',
...
For GAE I need to do:
'myproject.app1',
'myproject.app2',
...
For local development, both approaches work. When deploying to GAE, only the approach using the full pathname works. Why is this??
--
EDIT:
I tried the answer supplied by lucernia but ran into all kinds of problems. The major stumbling block is the deployment to GAE which will not run when app.yaml
is not in the root directory of the structure one tries to deploy.
I tried the answer referred to by Lipis which worked quite well. I had to add the following lines:
import sys
sys.path.insert(1, 'myproject')
to both my main.py
and settings.py
. Just adding it to main.py
was not enough. GAE was still not able to find my apps.
I thought I read Google's docs on GAE quite well but I either must have missed this or it is buried somewhere... Very important if one wants to run Django on GAE...