Reputation: 4797
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...
Upvotes: 0
Views: 775
Reputation: 4797
After some thought I decided to go with a solution as suggested by Lipis in his comment. This solves the import issue in Google App Engine and at the same time allows me to retain the default project structure as setup by Django's startproject
.
Upvotes: 1
Reputation: 6617
I meet the same issue before.
The root page for GAE is the folder where you put your app.yaml
.
So you probably put the app.yaml in the parent myproject
folder
but put the django project in the child myproject
folder.
In that case, while the development server is started with django.py runserver, it will have different behavior compare to started the with dev_appserver.py and GAE runtime.
In my case, I put my app.yaml in the same folder as settings.py and wsgi.py, urls.py. Then I can import my module without any problem.
myproject
- admin_system/
- __init__.py
- models.py
- app.yaml
- wsgi.py
- urls.py
- settings.py
- manager.py
in the settings.py
INSTALLED_APPS = (
'django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.sites',
'django.contrib.messages',
'django.contrib.staticfiles',
# Uncomment the next line to enable the admin:
'django.contrib.admin',
# Uncomment the next line to enable admin documentation:
'django.contrib.admindocs',
'admin_system',
)
in the wsgi.py
import os
os.environ.setdefault("DJANGO_SETTINGS_MODULE", "settings")
Then import path will be the same on both development and GAE runtime.
import admin_system.models
While moving your app.yaml to the inner myproject folder, the inner project directory becomes your working directory for GAE. So if you point google app engine luncher to the inner myproject folder, it should work without problem.
Or to make thing easier, you can just delete the outer myproject folder and use the inner myproject folder directly.
[myproject]
|
-[app1]
|
-[app2]
|
-app.yaml
-urls.py
-wsgi.py
-settings.py
-manager.py
Upvotes: 2