Apache loads x.wsgi file from django project, no __file__ attribute associated - python

I am trying to load a python wsgi file called from apache with mod_wsgi. My wsgi file attempts to reset the sys.path with:
import os, sys
root = os.path.join(os.path.dirname(__file__), '..')
sys.path.insert(0, root)
Apache complains with:
[Wed Feb 15 19:12:26 2012] [error] [client 127.0.0.1]
ImportError: Could not import settings 'mysite.settings'
(Is it on sys.path?): No module namedmysite.settings`
when I do:
>>> dir('mysite.wsgi')
It becomes apparent the wsgi file does not possess the __file__ attribute, so my sys.path is not getting updated with the necessary directory.
Why would this mysite.wsgi file not have a __file__ attribute?

It should work,
but you can also try and check if following code block helps.
DIRNAME = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
sys.path.append(DIRNAME)

The resulting path set to 'root', must be the parent directory of the directory where the Django settings.py is located and 'mysite' must be the name of that directory. You say nothing about the name of your project directory or where the WSGI file is located with respect to it, so not possible to tell you how your code is wrong.
Code under Apache will also run as special user, so project directory must be accessible/readable to that user.
Perhaps go watch:
http://code.google.com/p/modwsgi/wiki/WhereToGetHelp?tm=6#Conference_Presentations
to better understand how to set it all up.

Related

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

Configure WSGI with Django on OpenShift

I tried to configure WSGI with wsgi.py in my project folder
import os
import sys
sys.path.append(os.path.join(os.environ['OPENSHIFT_REPO_DIR']))
os.environ['DJANGO_SETTINGS_MODULE'] = 'project.settings'
virtenv = os.environ['OPENSHIFT_HOMEDIR'] + 'python/virtenv/'
os.environ['PYTHON_EGG_CACHE'] = os.path.join(virtenv, 'lib/python2.7/site-packages')
virtualenv = os.path.join(virtenv, 'bin/activate_this.py')
try:
execfile(virtualenv, dict(__file__=virtualenv))
except IOError:
pass
#
# IMPORTANT: Put any additional includes below this line. If placed above this
# line, it's possible required libraries won't be in your searchable path
#
import django.core.handlers.wsgi
application = django.core.handlers.wsgi.WSGIHandler()
but I have Internal Server Error 500
[Wed Jun 08 16:42:46 2016] [error] [client 127.9.155.129] ImportError: No module named project.settings
and I have this module available in my projet.
I tried some help like :
http://www.appsembler.com/blog/django-deployment-using-openshift/
How to configure Django on OpenShift?
Can you help my to launch my app on OpenShift
Thanks
I had on the top of wsgi.py
os.path.join(os.environ['OPENSHIFT_HOMEDIR'], 'app-root/repo')
and I didn't have this error !
And I also put all my code under folder which have the name of my project in order to match
os.environ['DJANGO_SETTINGS_MODULE'] = 'project.settings'

Django: adding a random.py file in project dir destroyed the setting's import in WSGI

This seems weird but it has happened a number of times now. I have some artitrary .py file which was just added to the "Project/Project" directory (same directory as my settings.py). 2-3 scripts import from this script.
After adding the script and restarting apache, the django website shows the following errors exclusively:
Error from:
raise ImportError("Could not import settings '%s' (Is it on sys.path?): %s" %
(self.SETTINGS_MODULE, e))
[Wed Jul 24 08:10:03 2013] [error] [client 127.0.0.1] ImportError:
Could not import settings 'Project.settings' (Is it on sys.path?):
No module named settings
Any advice? Thank you.
Here is a directory repr:
Project/
Project/
__init__.py
settings.py
urls.py
views.py
wsgi.py
something.py
App/
__init__.py
views.py
models.py
tasks.py
I tried to restore the site by removing something.py and its imports, however nothing worked.
UPDATE: in the wsgi.py file I put
print os.getcwd()
# prior to
os.environ.setdefault("DJANGO_SETTINGS_MODULE", "Project.settings")
from django.core.wsgi import get_wsgi_application
application = get_wsgi_application()
And the result was off! We were in the wrong directory.
It pointed to /home/user/path/to/django/project instead of /home/user, but however it still
does not work!! Which makes no sense because settings.py is right there, it can blatantly import it..
How come this random scenario caused this?? And previous times it did not.
Can someone please explain? Thanks.
WOW, unbelievable. I accidentally had an extra unneeded init.py inside the main higher level Project folder... This was causing Project.settings to look at the directory one level too high!!

ImportError - Django and mod_wsgi

I'm attempting to set up mod_wsgi with Django and Apache locally on a Fedora 16 machine. I run into the error:
ImportError: Could not import settings 'cat.settings' (Is it on sys.path?): No module named cat.settings
I realize there are a few other questions about this - but their solutions have not fixed this error. I appreciate any help or ideas you may have regarding the message!
--
Here's a bit of insight into my set-up:
Receiving 500 Internal Server Error at localhost.
My directory is: /home/name/src/django/animals/cat (where the cat directory contains an __init__.py and a settings.py file.
I have one application folder in the cat directory, named catOne - it also contains an __init__.py file.
My wsgi file looks like this:
import os
import sys
sys.path.append('/home/name/src/django/animals/cat')
sys.path.append('/home/name/src/django/animals')
sys.stderr.write('\n'.join(sys.path))
root = os.path.join(os.path.dirname(__file__), '..')
sys.path.insert(0, root)
packages = os.path.join(root, 'environ/lib/python2.7/site-packages')
sys.path.insert(0, packages)
os.environ['DJANGO_SETTINGS_MODULE'] = 'cat.settings'
import django.core.handlers.wsgi
application = django.core.handlers.wsgi.WSGIHandler()
The permissions on the settings and init files are 755, but I've also tried 777 without success.
My sys.path looks like:
/var/www
/usr/lib/python27.zip
/usr/lib/python2.7
/usr/lib/python2.7/plat-linux2
/usr/lib/python2.7/lib-tk
/usr/lib/python2.7/lib-old
/usr/lib/python2.7/lib-dynload
/usr/lib/python2.7/site-packages
/usr/lib/python2.7/site-packages/PIL
/usr/lib/python2.7/site-packages/gst-0.10
/usr/lib/python2.7/site-packages/gtk-2.0
/usr/lib/python2.7/site-packages/setuptools-0.6c11-py2.7.egg-info
/home/name/src/django/animals/cat
/home/name/src/django/animals
Thanks again for your help!
SELinux was causing an issue accessing the file. This probably isn't the best way, but I disabled it entirely by editing the /etc/selinux/config file. Change SELINUX=enforcing to SELINUX=disabled.
Again, there are probably finer grain controls for changing SELinux so disable at your own peril.

Categories