I'm trying to deploy my django project on a shared hosting as describe here
I have my project on /home/user/www/testa
I'm using this script
#!/usr/bin/python
import sys, os
sys.path.append("/home/user/bin/python")
sys.path.append('/home/user/www/testa')
os.chdir("/home/user/www/testa")
os.environ['DJANGO_SETTINGS_MODULE'] = "settings.py"
from django.core.servers.fastcgi import runfastcgi
runfastcgi(method="threaded", daemonize="false")
And here's the error I get when trying to run it from shell:
WSGIServer: missing FastCGI param REQUEST_METHOD required by WSGI!
WSGIServer: missing FastCGI param SERVER_NAME required by WSGI!
WSGIServer: missing FastCGI param SERVER_PORT required by WSGI!
WSGIServer: missing FastCGI param SERVER_PROTOCOL required by WSGI!
Traceback (most recent call last):
File "build/bdist.linux-i686/egg/flup/server/fcgi_base.py", line 558, in run
File "build/bdist.linux-i686/egg/flup/server/fcgi_base.py", line 1118, in handler
File "/home/user/lib/python2.4/site-packages/django/core/handlers/wsgi.py", line 230, in __call__
self.load_middleware()
File "/home/user/lib/python2.4/site-packages/django/core/handlers/base.py", line 33, in load_middleware
for middleware_path in settings.MIDDLEWARE_CLASSES:
File "/home/user/lib/python2.4/site-packages/django/utils/functional.py", line 269, in __getattr__
self._setup()
File "/home/usr/lib/python2.4/site-packages/django/conf/__init__.py", line 40, in _setup
self._wrapped = Settings(settings_module)
File "/home/user/lib/python2.4/site-packages/django/conf/__init__.py", line 75, in __init__
raise ImportError, "Could not import settings '%s' (Is it on sys.path? Does it have syntax errors?): %s" % (self.SETTINGS_MODULE, e)
ImportError: Could not import settings 'settings.py' (Is it on sys.path? Does it have syntax errors?): No module named settings.py
Content-Type: text/html
Unhandled Exception
Unhandled Exception
An unhandled exception was thrown by the application.
What am I doing wrong?
Running the script from the browser just gives me an internal server error.
The line
os.environ['DJANGO_SETTINGS_MODULE'] = "settings.py"
should be more like
os.environ['DJANGO_SETTINGS_MODULE'] = "settings"
based on how you're setting up sys.path. That environment variable is supposed to contain the path to the module as it should be imported by Python, not the actual filename of the module.
Basically, the way you've got it now is making Django do something like this internally:
import settings.py
I.e., it's trying to import a py module from inside a settings module.
Related
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()
I have a project in Django 1.1.4 and I am trying to set up this project on production with mod_wsgi but I am getting some errors :
My wsgi file code :
import os
import sys
import site
# Add the site-packages of the chosen virtualenv to work with
site.addsitedir('path_to_\site-packages')
# Add the app's directory to the PYTHONPATH
sys.path.append('path_to_dir')
sys.path.append('path_to_dir')
os.environ['DJANGO_SETTINGS_MODULE'] = 'settings'
import django.core.handlers.wsgi
application = django.core.handlers.wsgi.WSGIHandler()
I have tried to debug the error like when I print something in my settings It doesn't get print so I guess my settings is not being called.
My wsgi and settings file are in the same level and I have used two ways to mention settings in my wsgi file like os.environ['DJANGO_SETTINGS_MODULE'] = 'settings' and os.environ['DJANGO_SETTINGS_MODULE'] = 'project.settings' but both fails.
.
error - log :
File "C:\\Python27\\lib\\site-packages\\django\\core\\handlers\\wsgi.py", line 230, in __call__
self.load_middleware()
File "C:\\Python27\\lib\\site-packages\\django\\core\\handlers\\base.py", line 33, in load_middleware
for middleware_path in settings.MIDDLEWARE_CLASSES:
File "C:\\Python27\\lib\\site-packages\\django\\utils\\functional.py", line 272, in __getattr__
self._setup()
File "C:\\Python27\\lib\\site-packages\\django\\conf\\__init__.py", line 40, in _setup
self._wrapped = Settings(settings_module)
File "C:\\Python27\\lib\\site-packages\\django\\conf\\__init__.py", line 75, in __init__
raise ImportError, "Could not import settings '%s' (Is it on sys.path? Does it have syntax errors?): %s" % (self.SETTINGS_MODULE, e)
ImportError: Could not import settings 'cbc_website.settings' (Is it on sys.path? Does it have syntax errors?): No module named cbc_website.settings
Python is interpreting your '\' in the path definitions as an escape character. https://docs.python.org/2/reference/lexical_analysis.html#string-literals
Use r'path' when specifying a literal path with backslashes. So instead of:
sys.path.append('C:\\\something')
You specify:
sys.path.append(r'C:\\\something')
in your wsgi.py file.
Have you tried to import yourprojectname.settings instead of just settings ?
This is what I remenber doing, and what is specified in the link https://github.com/django/django/blob/1.1.4/docs/howto/deployment/modwsgi.txt provided in comments.
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've been stuck on this problem for what seems like forever, and none of the other threads I've found seem to help.
When I run python manage.py runserver in cmd, I get this output:
C:\Documents and Settings\ltiokhin\My Documents\DJCode\mysite>python manage.py runserver
Validating models...
0 errors found
Django version 1.4.3, using settings 'settings'
Development server is running at http:___________________________
Quit the server with CTRL-BREAK.
Unhandled exception in thread started by <bound method Command.inner_run of <django.contrib.staticfiles.management.commands.runserver.Command object at 0x010689 F0>>
Traceback (most recent call last):
File "C:\Python27\lib\site-packages\django\core\management\commands\runserver.
py", line 109, in inner_run
handler = self.get_handler(*args, **options)
File "C:\Python27\lib\site-packages\django\contrib\staticfiles\management\commands\runserver.py", line 24, in get_handler
handler = super(Command, self).get_handler(*args, **options)
File "C:\Python27\lib\site-packages\django\core\management\commands\runserver.py", line 39, in get_handler
return get_internal_wsgi_application()
File "C:\Python27\lib\site-packages\django\core\servers\basehttp.py", line 58, in get_internal_wsgi_application
"could not import module '%s': %s" % (app_path, module_name, e)) django.core.exceptions.ImproperlyConfigured: WSGI application 'mysite.wsgi.appli cation' could not be loaded; could not import module 'mysite.wsgi': No module na med mysite.wsgi
Thanks in advance for the help.
Do you use the same version as in the book?
Last time I had this kind of error it was because of that!
Or did you add wsgi to the installed_apps in settings.py?
Look for "wsgi.py" under the folder "C:\Documents and Settings\ltiokhin\My Documents\DJCode\mysite\mysite". Its possible that the standard django application file is missing or its named as something else.
I was trying to deploy django on a shared webhost which is not django specific. Host offers old version of python installed, but since I have ssh access ability, I've managed to extend python installation with modules I need (including django) by installing them locally in my home folder.
Ok, so I've created django project, did tweaks that needed to do (setting up PYTHONPATH and PATH globals etc.), made django.fcgi script which starts django and did ./django.fcgi from shell.
This is the response:
WSGIServer: missing FastCGI param REQUEST_METHOD required by WSGI!
WSGIServer: missing FastCGI param SERVER_NAME required by WSGI!
WSGIServer: missing FastCGI param SERVER_PORT required by WSGI!
WSGIServer: missing FastCGI param SERVER_PROTOCOL required by WSGI!
Traceback (most recent call last):
File "/home/tentacle/lib/python2.4/site-packages/flup-1.0.3.dev_20110405-py2.4.egg/flup/server/fcgi_base.py", line 574, in run
protocolStatus, appStatus = self.server.handler(self)
File "/home/tentacle/lib/python2.4/site-packages/flup-1.0.3.dev_20110405-py2.4.egg/flup/server/fcgi_base.py", line 1159, in handler
result = self.application(environ, start_response)
File "/home/tentacle/lib/python2.4/site-packages/django/core/handlers/wsgi.py", line 272, in __call__
response = self.get_response(request)
File "/home/tentacle/lib/python2.4/site-packages/django/core/handlers/base.py", line 169, in get_response
response = self.handle_uncaught_exception(request, resolver, sys.exc_info())
File "/home/tentacle/lib/python2.4/site-packages/django/core/handlers/base.py", line 202, in handle_uncaught_exception
from django.views import debug
File "/home/tentacle/lib/python2.4/site-packages/django/views/debug.py", line 9, in <module>
from django.template import (Template, Context, TemplateDoesNotExist,
File "/home/tentacle/lib/python2.4/site-packages/django/template/__init__.py", line 53, in <module>
from django.template.base import (ALLOWED_VARIABLE_CHARS, BLOCK_TAG_END,
MemoryError
Status: 500 Internal Server Error
Content-Type: text/html
<!DOCTYPE HTML PUBLIC "-//IETF//DTD HTML 2.0//EN">
<html><head>
<title>Unhandled Exception</title>
</head><body>
<h1>Unhandled Exception</h1>
<p>An unhandled exception was thrown by the application.</p>
</body></html>
So, I'm aware that this issue is about limited memory for my user (wrong?), but is it so low that I cannot run bare django?
Just for things to be worse, a month ago the same provider gave me a test account, and a was able not just to run django, but to install locally newer version of python and run it nice.
I did asked my web host support for help, but I didn't got any usable answer. Also since they are resellers I'm not very sure how much they help.
Is there a way to overcome this problem? Any suggestion is welcome.
Thanks in advance
-----------------------------------Update--------------------------------------------------
#!/home/<username>/bin/python
import os, sys
sys.path.insert(0, "/home/<username>/djangoprojects")
sys.path.insert(0, "/home/<username>/djangoprojects/testproject")
os.environ['PATH']= "/home/<username>/bin:"+os.environ['PATH']
os.environ['DJANGO_SETTINGS_MODULE'] = 'testproject.settings'
from django.core.servers.fastcgi import runfastcgi
runfastcgi(method="threaded", daemonize="false")
This is how look like my configuration. I must admit that there was a little error (interpreter path), but 'settings' file path was good.
After correcting the interpreter path to right one, First I've got StringError (some triple quoted doc string in one of djangos files was not closed - hm, hardly), than in next run MemoryError, and again MemoryError and so on. After a while (an hour) I tried to execute script again (no further changes made) and constantly ending up with Segmentation fault (core dumped).
Any suggestions?
When running it from the command line, you need to specify the Django parameters explicitly. Assuming you are running the script from the same folder as your settings.py is in:
import os, sys
sys.path.append(os.path.dirname(os.path.abspath(__file__)))
os.environ['DJANGO_SETTINGS_MODULE'] = 'settings'
# ... rest of your startup script...
Also, check if your .htaccess file still has a correct RewriteRule to your .fcgi script.