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.
Related
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
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.
I'm setting up a site in django with multiple settings files as recommended in 2 scoops of django where settings.py is replaced with a settings directory that contains multiple settings files (local.py, etc.). My development server will run fine when I leave settings.py in the default location, but when I move it into the new settings directory I created and run the development server with:
python manage.py runserver --settings=mysite.settings.local
I get the error:
ImportError: Could not import settings 'mysite.settings.local' (Is it on sys.path?): No module named settings.local
I have added the directory to the python path and it shows up when I go into python and enter:
import sys
sys.path
I am running out of ideas. Any help would be great.
try touch __init__.py in your settings directory
Python needs to have an __init__.py file in your root directory so it can consider the subfolders as importable.
Please try adding __init__.py to your root directory and run
python manage.py runserver in the terminal
```
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.
I am in the process of migrating my Django project dev environment to Eclipse.
I have Python and Django working properly in Eclipse. However, when I try to add external libraries, the project doesn't find them.
Here is how I have been adding the modules required:
Right-clicking on project and selecting Properties
Selecting the External Libraries tab
Either adding the source folder (which is in my virtual environment) or the egg (which is also there).
However, in both cases when I do this (for the Messages module) I get this error when doing a syncdb:
Error: No module named messages
I have tried restarting Eclipse, but still no luck.
I'm new to django, so that may be not a best practice, but it works fine for me:
Are you using PyDev? If yes, your external libraries are stored in your_workspace/your_project/.pydevproject file like this:
<path>/usr/local/lib/python2.7/dist-packages/django_annoying-0.7.6-py2.7.egg</path>
It seems to me that this paths are used in development time for code completion and checking types and so on... but they must be in your pythonpath when your run your project and Eclipce doesn't put them to PATH automatically.
To ensure that django apps are in PATH I add something like that to my settings_local.py:
# v PATHS SETTINGS v #
import os
import sys
PROJECT_ROOT = os.path.join(os.path.dirname(__file__), '..')
# v 3d-party django-apps v #
DJANGO_APPS_ROOT = os.path.join(PROJECT_ROOT, '..', '..', 'djaddons')
sys.path.insert(0, os.path.join(DJANGO_APPS_ROOT, 'south'))
sys.path.insert(0, os.path.join(DJANGO_APPS_ROOT, 'annoying'))
if CONF_APP_REGISTRATION_ENABLED:
sys.path.insert(0, os.path.join(DJANGO_APPS_ROOT, 'registration'))
# v python libs v #
PYTHON_LIBS_ROOT = os.path.join(PROJECT_ROOT, '..', '..', 'pylibs')
sys.path.insert(0, os.path.join(PYTHON_LIBS_ROOT, 'oauth2'))
sys.path.insert(0, os.path.join(PYTHON_LIBS_ROOT, 'httplib2'))
# ^ END OF PATHS SETTINGS ^ #
(it's a part of my settings.py which is specific for environment and differs on development computer and on the server)
Or you can add sys.path.insert statements to your .wsgi file on server (if using mod_wsgi) and to your manage.py file for testing with manage.py runserver
The most common thing would be that you're adding the wrong paths... compare what you have in the command line with the paths you really added inside Eclipse by running:
import sys
print('\n'.join(sorted(sys.path)))
and fix the paths inside Eclipse.