Question

I am deploying the django site with Apache2. When I was trying to execute manage.py, it goes to

ImportError: Could not import settings 'mysite.settings'   No module named mysite.settings

This is my project structure:

healthtweets/
manage.py
    code/
    __init__.py
        health_tweets/
        wsgi.py
        settings.py
        models.py
        __init__.py

I was trying to add all possible python paths, this is my wsgi.py: import os import sys from site import addsitedir from django.core.handlers.wsgi import WSGIHandler

sys.path.insert(0,'/home/usr/webapps/healthtweets_dev/healthtweets/')
sys.path.append('/home/usr/webapps/healthtweets_dev/healthtweets/code/health_tweets')
sys.path.append('/home/usr/webapps/healthtweets_dev/healthtweets/code/')
root = os.path.join(os.path.dirname(__file__), '..')  
sys.path.insert(0, root)  
os.environ['DJANGO_SETTINGS_MODULE'] = 'health_tweets.settings'
#application = django.core.handlers.wsgi.WSGIHandler()
application = WSGIHandler()

And this is the current python path:

['/home/usr/webapps/healthtweets_dev/healthtweets/code/health_tweets/..',
 '/home/usr/webapps/healthtweets_dev/healthtweets/',
 '/home/usr/webapps/healthtweets_dev/lib/python2.7/python_memcached-1.48-py2.7.egg',
 '/home/usr/webapps/healthtweets_dev/healthtweets/code',
 '/home/usr/webapps/healthtweets_dev/lib/python2.7',
 '/home/usr/webapps/django/lib/python2.7/python_memcached-1.48-py2.7.egg',
 '/home/usr/lib/python2.7/pymongo-2.3-py2.7-linux-x86_64.egg',
 '/home/usr/lib/python2.7/django_picklefield-0.2.1-py2.7.egg',
 '/home/usr/webapps/django/lib/python2.7/python_memcached-1.48-py2.7.egg',
 '/home/usr/lib/python2.7',
 '/home/usr/webapps/healthtweets_dev/healthtweets/manage.py',
 '/home/usr',
 '/home/usr/webapps/django/lib/python2.7',
 '/usr/local/lib/python27.zip',
 '/usr/local/lib/python2.7',
 '/usr/local/lib/python2.7/plat-linux2',
 '/usr/local/lib/python2.7/lib-tk',
 '/usr/local/lib/python2.7/lib-old',
 '/usr/local/lib/python2.7/lib-dynload',
 '/usr/local/lib/python2.7/site-packages',
 '/usr/local/lib/python2.7/site-packages/PIL',
 '/home/usr/webapps/healthtweets_dev/healthtweets/manage.py',
 '/home/usr/webapps/healthtweets_dev/healthtweets/code/health_tweets',
 '/home/usr/webapps/healthtweets_dev/healthtweets/code/']

This is the httpd.conf:

 ServerRoot "/home/usr/webapps/healthtweets_dev/apache2"

 LoadModule dir_module        modules/mod_dir.so
 LoadModule env_module        modules/mod_env.so
 LoadModule log_config_module modules/mod_log_config.so
 LoadModule mime_module       modules/mod_mime.so
 LoadModule rewrite_module    modules/mod_rewrite.so
 LoadModule setenvif_module   modules/mod_setenvif.so
 LoadModule wsgi_module       modules/mod_wsgi.so


 LogFormat "%{X-Forwarded-For}i %l %u %t \"%r\" %>s %b \"%{Referer}i\" \"%{User-Agent}i\"" combined
 CustomLog /home/usr/logs/user/access_healthtweets_dev.log combined
 ErrorLog /home/usr/logs/user/error_healthtweets_dev.log
 KeepAlive Off
 Listen 31846
 MaxSpareThreads 3
 MinSpareThreads 1
 ServerLimit 1
 SetEnvIf X-Forwarded-SSL on HTTPS=1
 ThreadsPerChild 5
 WSGIPythonPath    /home/usr/webapps/healthtweets_dev/healthtweets/code/:/home/usr/webapps/healthtweets_dev/lib/python2.7
 WSGIDaemonProcess healthtweets_dev processes=2 threads=12 python-path= /home/usr/webapps/healthtweets_dev/healthtweets/code/:/home/usr/webapps/healthtweets _dev/lib/python2.7
 WSGIProcessGroup healthtweets_dev
 WSGIRestrictEmbedded On
 WSGILazyInitialization On
 WSGIScriptAlias / /home/usr/webapps/healthtweets_dev/healthtweets/code/health_tweets/wsgi.py

Beside this, I have also chmod 711 to the project directories. But nothing works. Thank you very much!

Edit: I tested the paths as described in this post: How to troubleshoot - ImportError: Could not import settings 'mysite.settings' when deploying django? They have all existed...

>>> os.path.exists('/home/usr/webapps/healthtweets_dev/healthtweets/code/health_tweets')
True
>>> os.path.exists('/home/usr/webapps/healthtweets_dev/healthtweets/code/health_tweets/settings.py')
True
>>> os.path.exists('/home/usr/webapps/healthtweets_dev/healthtweets/code/')
True
>>> os.path.exists('/home/usr/webapps/healthtweets_dev/healthtweets/')
True
>>> os.path.exists('/home/usr/webapps/healthtweets_dev/')
True

But the final test is not passed:

>>> import health_tweets.settings
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
    ImportError: No module named health_tweets.settings
Was it helpful?

Solution

Your directories layout is badly formatted so it's hard to tell where your settings lives in healthtweets or in healthtweets/code/health_tweets, from your comment I assume it's the second. If so, you need to have /home/usr/webapps/healthtweets_dev/healthtweets/ in your sys.path (preferably first), and set DJANGO_SETTINGS_MODULE as code.health_tweets.settings, or have /home/usr/webapps/healthtweets_dev/healthtweets/code in your sys.path and set DJANGO_SETTINGS_MODULE as health_tweets.settings.

If none of the above solutions work, you either

  • have some other code.health_tweets (first case) or health_tweets (second case) module or package in your sys.path shadowing the one you're expecting or
  • have a permission problem (remember it needs to be readable for your WSGIDaemonProcess user)

The general rule is that DJANGO_SETTINGS_MODULE is supposed to be a Python qualified (sub)module path, and that the directory containing the root if this Python qualified path must be in your sys.path.

Also adding things in WSGIPythonPath plus WSGIDaemonProcess --python-path plus messing with sys.path in your .wsgi script will only add to confusion.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top