Hosting Django with passenger_wsgi.py - python

I'm having trouble setting up my django on dreamhost shared hosting using python passenger_wsgi.py and virtual env. When I run passenger_wsgi.py no error is returned and the shell prints my project path. My website shows a 500 International server error. I have cleared my cache so there is no cache error. How do I set this up properly?
django 1.9
python 2.7
apache
My site structure is:
/home/myuser/mydomain.com/
env/
myApp/
passenger_wsgi.py
public/
passenger_wsgi.py
import sys, os
cwd = os.getcwd()
sys.path.append(cwd)
project_location = cwd + '/myApp'
print (project_location)
sys.path.insert(0, project_location)
#Switch to new python
if sys.version < "2.7.3": os.execl("/home/myuser/mydomain.com/env/bin/python",
"python2.7.3", *sys.argv)
sys.path.insert(0,'/home/myuser/mydomain.com/env/bin')
sys.path.insert(0,'/home/myuser/mydomain.com/env/lib/python2.7/site-packages/django')
sys.path.insert(0,'/home/myuser/mydomain.com/env/lib/python2.7/site-packages')
os.environ['DJANGO_SETTINGS_MODULE'] = "myApp.settings"
import django.core.handlers.wsgi
application = django.core.handlers.wsgi.WSGIHandler()

I had issues with Dreamhost specifically when upgrading from Django 1.6 to 1.8. One of the issues was with the WSGIHandler(). I can't say this is your problem specifically, but you can try setting application like this:
from django.core.wsgi import get_wsgi_application
application = get_wsgi_application()
Finally - make sure you restart passenger. There are docs here about how to do it: http://wiki.dreamhost.com/Passenger
From that page:
Whenever the code or configuration files for your application are modified, you must create or update the modification date of the file "tmp/restart.txt" in the application's root directory tree in order to trigger Passenger to reinitialize the application. Passenger caches many resources so changes are not recognized unless the modification date of "tmp/restart.txt" is changed.
The most common method to make this change is to run "touch tmp/restart.txt" via SSH. (Ruby on Rails automatically creates a directory named "tmp". If you are creating non-RoR application, you may need to create the "tmp" directory manually.

Related

IIS can not access newly added files in a django project

In a django project that is served by IIS (windows), i added a local file, test.py. The project ran perfectly before and still runs perfect on localhost, however IIS appears to not recognize the new test.py file. It appears IIS access to this file fails, even if the users IUSR and IIS_USRS have full access (same as for all other files in the folder).
I get below error message, and somethimes also the same but "No module named 'app.test'. Removing the "import app.test as test" in views.py solves the issue.
Suprisingly the "import app.oldFile as oldFile" works without issue.
In my views.py, i import the python scripts like this
import app.oldFile as oldFile
import app.test as test
My django project has the structure:
djangoRest
-app
--__init__.py
--views.py
--oldFile.py
--test.py
-djangoRest
--__init__.py
--settings.py
--urls.py
--wsgi.py
I found the reason, and solution.
In the earlier import file oldFile.py i use the line
import os
os.chdir(r\\some\\other\\directory\\)
This obviously changes the current working directory and as a result the other file test.py can't be found, and cannot be imported.
This mistake does not matter when testing django on the localhost, but only manifests in an issue when served by IIS.

Error Running WSGI application - client_secrets.json

I´m making a web app with django, but when I run the application, I´m getting this error:
I have uploaded my client_secrets.json file in the project path and I´m sure I have no typos
Settings.py
GOOGLE_OAUTH2_CLIENT_SECRETS_JSON = 'client_secrets.json'
WSGI.py
# This file contains the WSGI configuration required to serve up your
# web application at http://bohosul02.pythonanywhere.com/
# It works by setting the variable 'application' to a WSGI handler of some
# description.
#
# The below has been auto-generated for your Django project
import os
import sys
# add your project directory to the sys.path
project_home = '/home/bohosul02/misitio'
if project_home not in sys.path:
sys.path.insert(0, project_home)
# set environment variable to tell django where your settings.py is
os.environ['DJANGO_SETTINGS_MODULE'] = 'gfglogin.settings'
# serve django via WSGI
from django.core.wsgi import get_wsgi_application
application = get_wsgi_application()
You're using a relative path name (client_secrets.json) without paying attention to what your working directory is. If you use the full path to the file, then it will be able to find it.

Django's "check" cannot import settings

Since the project is evolving I would like to start executing the system check framework of Django on dev environment. The technology stack is Ubuntu, PostgreSQL, Django1.9 + UWSGI. But...
django-admin check
outputs the following error:
ImportError: No module named my_project.settings
The wsgi.py file contains:
import os
import sys
from django.core.wsgi import get_wsgi_application
sys.path.append('~/virtenv/my_site')
os.environ.setdefault("DJANGO_SETTINGS_MODULE", "my_site.settings")
application = get_wsgi_application()
The env variable DJANGO_SETTINGS_MODULE echoes my_site.settings
It is important to mention that the settings.py file is in virtenv/my_site/my_site/ . Please also note that the entire web application is running fine, I am also using the features of manage.py. Its just the django-admin check that is getting on my nerves.
you seem to append to the sys path in the wsgi but the admin check won't be using that.
Make sure you add the project path to the sys path for the environment you're using

django import local settings from the server

In my home directory, I have a folder called local. In it, there are files called __init__.py and local_settings.py. My django app is in a completely different directory. When the app is NOT running in DEBUG mode, I want it to load the local_settings.py file. How can this be acheived? I read the below:
Import a module from a relative path
Importing files from different folder in Python
http://docs.python.org/tutorial/modules.html
Basically, those tutorials are allowing to import from another directory, but what about a completely different working tree? I don't want to keep doing .., .., .. etc. Is there a way to goto the home directory?
I tried the following code:
import os, sys
os.chdir(os.path.join(os.getenv("HOME"), 'local'))
from local_settings import *
But i keep seeing errors in my apache error.log for it...
os.chdir just affects the current working directory, which has nothing whatsoever to do with where Python imports modules from.
What you need to do is to add the the local directory to the Pythonpath. You can either do this from the shell by modifying PYTHONPATH, or from inside Python by modifying sys.path:
import sys
import os
sys.path.append(os.path.expanduser("~/local"))
import local_settings
In response to your concerns about source control, you can just set the source control to ignore that file, and/or have a symlink installed as part of your deploy script to link the file on the os into another. I do both , though not in django. but it's a trivial task.
your deploy layout could look like this:
/current ( symlink to /releases/v3 )
/settings/local_settings.py
/releases/v1
/releases/v2
/releases/v3
and a task runs as part of your deploy:
cd /current
ln -s /settings/local_settings.py local_settings.py
if you're deploying with fab or capistrano, it's a few lines of configuration. i'm sure you could do this with puppet/chef simply too.

Django settings.py Error: Import by filename is not supported

I am running Django in a virtual environment (using virtualenv), and I'm trying to add a custom development environment settings file to simplify app configuration when I'm developing. My plan was to do this with two lines of code
if os.environ.get('DEVELOPMENT', None):
from login import settings_dev
I've also tried import settings_def and from login.settings_dev import *. My settings_dev.py file is sitting in the same directory as my settings.py file and my app is sitting in a folder called login. When I run python login/manage.py syncdb I get this error:
Error: Import by filename is not supported.
My searching keeps bringing up DJANGO_SETTINGS_MODULE (though I'm not sure how it plays into all this - first Django app :]), so just an FYI it is set in my settings.py file like so:
os.environ['DJANGO_SETTINGS_MODULE'] = 'login.settings'
I've also tried exporting it in my terminal, but I get the same error.
Does anyone know how I can fix this/what I'm doing wrong here?
Make sure while passing relative address of file to use "." instead of "/".
I faced the same error what I actually did
"music/urls"
But it should be
"music.urls"
In the original settings.py, at the very end:
try:
from settings_dev import *
except ImportError:
pass
Create settings_dev.py in the same directory as settings.py, and in it, add these two lines at the very top:
import sys
globals().update(vars(sys.modules['settings']))
Now add whatever development settings you want in this file.
I had similar error in runserver command execution and finally I've found that this error raises because of python version incompatibility by the django version installed. There is two versions of python on my system and I had running django server by the wrong one. Hope it could be helpful to someone.

Categories