django_settings_module is undefined on GAE server - python

So, I know you guys have been asked this question a million times, believe me I think I have read them all. I am getting the DJANGO_SETTINGS_MODULE is undefined error when trying to convert the Django polls app (the one made in the django tutorial) to run on the google app engine using the google cloud SQL.
Here is the actual stack trace given to me by the google app engine logs:
Traceback (most recent call last):
File "/base/python27_runtime/python27_lib/versions/1/google/appengine/runtime/wsgi.py", line 223, in Handle
result = handler(dict(self._environ), self._StartResponse)
File "/base/python27_runtime/python27_lib/versions/third_party/django-1.4/django/core/handlers/wsgi.py", line 219, in __call__
self.load_middleware()
File "/base/python27_runtime/python27_lib/versions/third_party/django-1.4/django/core/handlers/base.py", line 39, in load_middleware
for middleware_path in settings.MIDDLEWARE_CLASSES:
File "/base/python27_runtime/python27_lib/versions/third_party/django-1.4/django/utils/functional.py", line 184, in inner
self._setup()
File "/base/python27_runtime/python27_lib/versions/third_party/django-1.4/django/conf/__init__.py", line 40, in _setup
raise ImportError("Settings cannot be imported, because environment variable %s is undefined." % ENVIRONMENT_VARIABLE)
ImportError: Settings cannot be imported, because environment variable DJANGO_SETTINGS_MODULE is undefined.
I was getting the same error when trying to run it on dev_appserver.py provided with the google app engine but managed to remedy that by adding
env_variables:
DJANGO_SETTINGS_MODULE: 'DjangoTut.settings'
to the app.yaml file.
my project directory looks like this:
DjangoTut
DjangoTut
init.py
settings.py
urls.py
wsgi.py
polls
init.py
admin.py
models.py
tests.py
urls.py
views.py
app.yaml
manage.py
and my app.yaml file looks like this
application: hochotime
version: 10
runtime: python27
api_version: 1
threadsafe: true
libraries:
- name: django
version: "1.4"
builtins:
- django_wsgi: on
env_variables:
DJANGO_SETTINGS_MODULE: 'settings'
every other file is identical to those suggested here https://docs.djangoproject.com/en/1.4/intro/tutorial01/
I have tried adding
import os
os.environ['DJANGO_SETTINGS_MODULE'] = 'DjangoTut.settings'
to the start of just about every single python file but to no avail. I have tried moving the settings file into the project directory rather than the app directory and changing all the os.environ['DJANGO_SETTINGS_MODULE'] to point to just 'settings'. I have also tried configuring the settings directly as shown here https://docs.djangoproject.com/en/dev/topics/settings/#using-settings-without-setting-django-settings-module but still, I get the same error whenever I try to run it on the google servers.
Any help would be greatly appreciated.

Are you using Django-nonrel? If not, take a look at this appengine ariticle.
I would try moving settings.py out to the same directory as app.yaml. The remove the env_variables from app.yaml, you shouldn't need those for it to work. However you will need handlers defined if you don't already (not sure if you listed your whole app.yaml or not):
handlers:
- url: /static
static_dir: static
- url: /.*
script: main.py
The environment setting should be in the wsgi handler you setup (main.py in this example) as shown in the google article above.
os.environ['DJANGO_SETTINGS_MODULE'] = 'settings'
Hope this gets you going down the right path.

Related

Python | Docker | Django | django.core.exceptions.ImproperlyConfigured Error

I'm trying to run a python file that is inside a container however I'm getting the following exception:
Traceback (most recent call last):
File "/tmp/e2esetup.py", line 2, in <module>
django.setup()
File "/usr/local/lib/python3.6/site-packages/django/__init__.py", line 17, in setup
configure_logging(settings.LOGGING_CONFIG, settings.LOGGING)
File "/usr/local/lib/python3.6/site-packages/django/conf/__init__.py", line 55, in __getattr__
self._setup(name)
File "/usr/local/lib/python3.6/site-packages/django/conf/__init__.py", line 41, in _setup
% (desc, ENVIRONMENT_VARIABLE))
django.core.exceptions.ImproperlyConfigured: Requested setting LOGGING_CONFIG, but settings are not configured. You must either define the environment variable DJANGO_SETTINGS_MODULE or call settings.configure() before accessing settings.
Code being run in docker:
docker exec -it $DOCKER_ID python /tmp/e2esetup.py
My code:
import django
django.setup()
from django.conf import settings
from django.contrib.auth.models import User
from apps.account.models import Profile
from apps.organisation.models import Organisation
....
#rest is fine for sure
I'm new to django and docker, from what I can tell I need to set the environment however I don't know how, running through manage.py does this for you or something, so if I were to run a python file through the interpreter, I have to do this manually, however I dont know how.
I also read that I need to pipe the docker environment somehow, but I'm not sure what that means either, any help is appreciated!
As the error suggests you need to set the DJANGO_SETTINGS_MODULE environment variable. It's value should be something like mysite.settings (check what it is in manage.py).
You can set it in the Dockerfile
ENV DJANGO_SETTINGS_MODULE mysite.settings
Or in the environment when you
docker exec -it -e "DJANGO_SETTINGS_MODULE=mysite.settings" $DOCKER_ID python /tmp/e2esetup.py
Or in the script itself.
import os
import django
os.environ.setdefault('DJANGO_SETTINGS_MODULE', 'mysite.settings')
django.setup()

Python ImportError: How to resolve "No module named main" in Google appengine project?

I have setup a google appengine project in my ubuntu system for the first time and made everything works till now by reading all the documentation available online. But here I have stuck, it is running properly on localhost using command:dev_appserver.py app.yaml but when I try to access my app on my browser, it throws the error: ImportError: No module named main
Project Structure
-GAE_Demo
-google_appengine
-src
-handlers.py
-main.py
-app.yaml
-config.json
app.yaml
url: /.*
script: main.app
secure: always
src/main.py
app = webapp2.WSGIApplication(
routes=([('/main', MainHandler)]),
debug=True)
Error
INFO 2017-10-17 01:32:58,464 module.py:821] default: "GET / HTTP/1.1" 500 -
ERROR 2017-10-17 01:56:21,976 wsgi.py:263]
Traceback (most recent call last):
File "/home/path/to/project/GAE_Demo/google_appengine/google/appengine/runtime/wsgi.py", line 240, in Handle
handler = _config_handle.add_wsgi_middleware(self._LoadHandler())
File "/home/path/to/project/GAE_Demo/google_appengine/google/appengine/runtime/wsgi.py", line 299, in _LoadHandler
handler, path, err = LoadObject(self._handler)
File "/home/path/to/project/GAE_Demo/google_appengine/google/appengine/runtime/wsgi.py", line 85, in LoadObject
obj = __import__(path[0])
ImportError: No module named main
Thanks in Advance !!
First, going off-script on your first App Engine project (by putting .py files in src/, which none of the tutorials I know of do), is setting you up for frustrations that are better avoided until you have a successful project under your belt.
The easy way forward is to move the files in src/ up a level. But if you really, really want to stick with src/, then try adding an __init__.py to src/, and change your app.yaml to
url: /.*
script: src.main.app
secure: always

AppEngine application using Django fails to load

Django is constantly causing our application to crash. After deployment the application is running fine, but once the initial instance is restarted/shutdown it often fails to start with an error similar to the following:
Traceback (most recent call last): File "/base/data/home/runtimes/python27/python27_lib/versions/1/google/appengine/runtime/wsgi.py", line 266, in Handle
result = handler(dict(self._environ), self._StartResponse)
File "/base/data/home/runtimes/python27/python27_lib/versions/third_party/django-1.5/django/core/handlers/wsgi.py", line 236, in call
self.load_middleware()
File "/base/data/home/runtimes/python27/python27_lib/versions/third_party/django-1.5/django/core/handlers/base.py", line 53, in load_middleware
raise exceptions.ImproperlyConfigured('Error importing middleware %s: "%s"' % (mw_module, e))
ImproperlyConfigured: Error importing middleware myfolder.middleware: "No module named myfolder.middleware"
Our file structure is similar to this:
|- app.yaml
|- _ _ init _ _.py
|- settings.py
|- myfolder |
| |- _ _ init _ _.py
| |- middleware.py
| |- ...
|-...
|
Our app.yaml:
application: XXXXX
module: app
version: master
runtime: python27
api_version: 1
threadsafe: true
handlers:
- url: /api/(login|logout|passwd|master.|banners.)
script: app.handler
secure: always
...
builtins:
- django_wsgi: on
libraries:
- name: django
version: 1.5
env_variables:
DJANGO_SETTINGS_MODULE: 'settings'
We have 2 modules in our application and they both exhibit this behaviour (they have similar configurations). Sometimes the modules will stay up for a whole day before crashing again. After they fail to load, all subsequent requests fail with he same error. Deploying one more time always solves the problem temporarily.
We are using plain django with CloudSql. The problem is not reproducible in the development server. After deployment everything in both modules works fine. All middleware, ndb, memcache, cloudsql, taskqueue, etc, including all the modules inside the "myfolder" and every other library xcopied.
The following attempts at solving this problem haven't worked:
We have tried using the appengine_config.py to force django to reload the settings with from django.conf import settings\nsettings._target = None\n
Originally we had shared settings inside "myfolder" and were importing them with "from myfolder.shared_settings import *" inside the root settings.py but django could not load the module myfolder.shared_settings either (similar problem)
using a custom mysettings.py and defining the DJANGO_SETTINGS_MODULE in the app.yaml or in python
The system is not live yet but will be soon and we are running out of options.
Other traces of similarly failing configurations:
Traceback (most recent call last):
File "/base/data/home/runtimes/python27/python27_lib/versions/1/google/appengine/runtime/wsgi.py", line 266, in Handle
result = handler(dict(self._environ), self._StartResponse)
File "/base/data/home/runtimes/python27/python27_lib/versions/third_party/django-1.5/django/core/handlers/wsgi.py", line 236, in __call__
self.load_middleware()
File "/base/data/home/runtimes/python27/python27_lib/versions/third_party/django-1.5/django/core/handlers/base.py", line 45, in load_middleware
for middleware_path in settings.MIDDLEWARE_CLASSES:
File "/base/data/home/runtimes/python27/python27_lib/versions/third_party/django-1.5/django/conf/__init__.py", line 53, in __getattr__
self._setup(name)
File "/base/data/home/runtimes/python27/python27_lib/versions/third_party/django-1.5/django/conf/__init__.py", line 48, in _setup
self._wrapped = Settings(settings_module)
File "/base/data/home/runtimes/python27/python27_lib/versions/third_party/django-1.5/django/conf/__init__.py", line 134, in __init__
raise ImportError("Could not import settings '%s' (Is it on sys.path?): %s" % (self.SETTINGS_MODULE, e))
ImportError: Could not import settings 'settings' (Is it on sys.path?): No module named myfolder.settings
Traceback (most recent call last):
File "/base/data/home/runtimes/python27/python27_lib/versions/1/google/appengine/runtime/wsgi.py", line 239, in Handle
handler = _config_handle.add_wsgi_middleware(self._LoadHandler())
File "/base/data/home/runtimes/python27/python27_lib/versions/1/google/appengine/api/lib_config.py", line 353, in __getattr__
self._update_configs()
File "/base/data/home/runtimes/python27/python27_lib/versions/1/google/appengine/api/lib_config.py", line 289, in _update_configs
self._registry.initialize()
File "/base/data/home/runtimes/python27/python27_lib/versions/1/google/appengine/api/lib_config.py", line 164, in initialize
import_func(self._modname)
File "/base/data/home/apps/s~blue-myapp/app:master.375531077560785947/appengine_config.py", line 17, in
settings._target = None
File "/base/data/home/runtimes/python27/python27_lib/versions/third_party/django-1.5/django/utils/functional.py", line 227, in __setattr__
self._setup()
File "/base/data/home/runtimes/python27/python27_lib/versions/third_party/django-1.5/django/conf/__init__.py", line 48, in _setup
self._wrapped = Settings(settings_module)
File "/base/data/home/runtimes/python27/python27_lib/versions/third_party/django-1.5/django/conf/__init__.py", line 134, in __init__
raise ImportError("Could not import settings '%s' (Is it on sys.path?): %s" % (self.SETTINGS_MODULE, e))
ImportError: Could not import settings 'settings' (Is it on sys.path?): No module named myfolder.settings
This is our current appengine_config.py:
import sys
import logging
logging.debug(",\n".join(sys.path))
# Since Google App Engine's webapp framework uses Django templates, Django will half-initialize when webapp is loaded.
# This causes the initialization of the rest of Django's setting to be skipped. If you are getting this error, you need
# to explicitly force Django to reload your settings:
from django.conf import settings
settings._target = None
Logging sys.path from appengine_config.py does not change between a successful instance start and a failed instance start (apart from the XXXXXXXXXXX bit of course):
/base/data/home/apps/s~blue-persomi/app:master.3759720XXXXXXXXXXX,
/base/data/home/runtimes/python27/python27_dist/lib/python27.zip,
/base/data/home/runtimes/python27/python27_dist/lib/python2.7,
/base/data/home/runtimes/python27/python27_dist/lib/python2.7/plat-linux2,
/base/data/home/runtimes/python27/python27_dist/lib/python2.7/lib-tk,
/base/data/home/runtimes/python27/python27_dist/lib/python2.7/lib-old,
/base/data/home/runtimes/python27/python27_dist/lib/python2.7/lib-dynload,
/base/data/home/runtimes/python27/python27_dist/lib/python2.7/site-packages,
/base/data/home/runtimes/python27/python27_lib/versions/1,
/base/data/home/runtimes/python27/python27_lib/versions/third_party/MySQLdb-1.2.4b4,
/base/data/home/runtimes/python27/python27_lib/versions/third_party/django-1.5,
/base/data/home/runtimes/python27/python27_lib/versions/third_party/protorpc-1.0,
/base/data/home/runtimes/python27/python27_lib/versions/third_party/webapp2-2.5.2,
/base/data/home/runtimes/python27/python27_lib/versions/third_party/webob-1.1.1,
/base/data/home/runtimes/python27/python27_lib/versions/third_party/yaml-3.10
It seems to be a path related issue as people have mentioned in your question's comments
Possible short-sighted solution add everything to your path manually - look at the top answer here:
How to import modules in Google App Engine?
At the very least, this will help narrow the problem to path related.
what the docs say: https://developers.google.com/appengine/docs/python/
The Python module include path includes your application's root
directory (the directory containing the app.yaml file). Modules you
create in your application's root directory are available using a path
from the root. Don't forget to create init.py files in
sub-directories, so Python will recognize the sub-directories as
packages.
so from what I can tell, b/c everything is at or below the app.yaml file in your question the path should already be correct.
double check all your __init__.py file files are in place and spelled correctly.
try deleting all of your *.pyc files and letting them be regenerated.
try importing from the container's folder name FOLDER_CONTAINING_YAML.myfolder.middleware
Add the following lines to your app.yaml
libraries:
- name: MySQLdb
version: "latest"
It is in the documentation here.
https://cloud.google.com/appengine/docs/python/cloud-sql/
Ideally it should have been documented in the GCM DJANGO guide. Would have saved me a lot of time.
Praveen

I am not able to connect to the database using django

I am able to connect to the database using MySQLdb , but not able to connect using the django
please any body help me out
All parameters are correct , like host,password and username.
when ever i tried import something python interpreter regarding django i am getting the following error
File "<stdin>", line 1, in <module>
File "/usr/local/lib/python2.6/dist-packages/django/db/__init__.py", line 14, in <module>
if not settings.DATABASES:
File "/usr/local/lib/python2.6/dist-packages/django/utils/functional.py", line 276, in __getattr__
self._setup()
File "/usr/local/lib/python2.6/dist-packages/django/conf/__init__.py", line 40, in _setup
raise ImportError("Settings cannot be imported, because environment variable %s is undefined." % ENVIRONMENT_VARIABLE)
ImportError: Settings cannot be imported, because environment variable DJANGO_SETTINGS_MODULE is undefined.
of course i have django installed.
please help me out , i am new to django....
Make sure you run python manage.py shell so that all django-specific environment variables are loaded.
If you just run python, then you'll get errors like the one you posted since the django environment is not correctly setup.
Import the value of DJANGO_SETTINGS_MODULE.
This can be either done by:
Adding the project directory to $PYTHONPATH.
Change DJANGO_SETTINGS_MODULE to projectfoldername.settings.
DJANGO_SETTINGS_MODULE - is the environment variable, set it to your projects settings.py file.
import os
os.environ['DJANGO_SETTINGS_MODULE'] = "yourprojectname.settings"
More info at : https://docs.djangoproject.com/en/dev/topics/settings/

main.py throws "ImportError: No module named app"

I have a file in the root directory of my app called main.py. This file contains the code which starts up the app:
app = webapp2.WSGIApplication(routes = routes, debug = True, config = config)
def main():
app.run()
if __name__ == '__main__':
main()
I have, amongst others, this in app.yaml:
- url: /.*
script: main.app
After upgrading to the 1.6.6 SDK, I get the following error:
ERROR 2012-06-05 13:39:01,856 wsgi.py:189]
Traceback (most recent call last):
File "/Users/jdoe/Applications/GoogleAppEngineLauncher.app/Contents/Resources/GoogleAppEngine-default.bundle/Contents/Resources/google_appengine/google/appengine/runtime/wsgi.py", line 187, in Handle
handler = _config_handle.add_wsgi_middleware(self._LoadHandler())
File "/Users/jdoe/Applications/GoogleAppEngineLauncher.app/Contents/Resources/GoogleAppEngine-default.bundle/Contents/Resources/google_appengine/google/appengine/runtime/wsgi.py", line 236, in _LoadHandler
__import__(cumulative_path)
ImportError: No module named app
If I rename main.py to anything else than main.py and update app.yaml accordingly, this error disappears, and I can run the app.
Why can I no longer have my main routines in a file called main.py? On the App Engine SDK download page they say they're now including an experimental version of Python 2.7 in the SDK, could this have something to do with the issue I'm experiencing?
My comment as an answer:
Solved this myself. In the same dir where main.py resides I had created a package also called main. In app.yaml, I'd specified to look in main.app. Seems that if a package called main exists in the same dir as main.py, Python goes to look inside the package for a module called app. But if no package named main exists, it happily looks in main.py.

Categories