Supervisor, Celery, Virtualenvwrapper, Django: Could not import django settings even when pythonpath added to environment

StackOverflow https://stackoverflow.com/questions/19928036

Question

Can anyone see what I'm doing wrong here? No matter what I try, I can't seem to set it to correctly detect the settings for Django. Works fine when being run manually in terminal in the active virtualenv.

Supervisor

[program:celery_beetlejuice]
command = /home/padraic/.virtualenvs/beetlejuice/bin/python /home/padraic/CodeDev/beetlejuice/beetlejuice_django/manage.py celeryd -B -E -l INFO
directory=/home/padraic/CodeDev/beetlejuice/beetlejuice_django
environment=PYTHONPATH="/home/padraic/CodeDev/beetlejuice/beetlejuice_django", DJANGO_SETTINGS_MODULE="beetlejuice_django.settings"
user = padraic
autostart=true
autorestart=true
stdout_logfile=/home/padraic/CodeDev/beetlejuice/beetlejuice_django/logs/celeryd.log
stderr_logfile=/home/padraic/CodeDev/beetlejuice/beetlejuice_django/logs/celeryd.log
redirect_stderr=true
priority=998
numprocs=1

Traceback

Traceback (most recent call last):
  File "manage.py", line 10, in <module>
    execute_from_command_line(sys.argv)
  File "/home/padraic/.virtualenvs/beetlejuice/local/lib/python2.7/site-packages/django/core/management/__init__.py", line 453, in execute_from_command_line
    utility.execute()
  File "/home/padraic/.virtualenvs/beetlejuice/local/lib/python2.7/site-packages/django/core/management/__init__.py", line 392, in execute
    self.fetch_command(subcommand).run_from_argv(self.argv)
  File "/home/padraic/.virtualenvs/beetlejuice/local/lib/python2.7/site-packages/django/core/management/__init__.py", line 263, in fetch_command
    app_name = get_commands()[subcommand]
  File "/home/padraic/.virtualenvs/beetlejuice/local/lib/python2.7/site-packages/django/core/management/__init__.py", line 109, in get_commands
    apps = settings.INSTALLED_APPS
  File "/home/padraic/.virtualenvs/beetlejuice/local/lib/python2.7/site-packages/django/conf/__init__.py", line 53, in __getattr__
    self._setup(name)
  File "/home/padraic/.virtualenvs/beetlejuice/local/lib/python2.7/site-packages/django/conf/__init__.py", line 48, in _setup
    self._wrapped = Settings(settings_module)
  File "/home/padraic/.virtualenvs/beetlejuice/local/lib/python2.7/site-packages/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 'beetlejuice_django.settings' (Is it on sys.path?): 
Était-ce utile?

La solution

Sigh... figured out the error...

So! I have my settings.py file which contains:

[settings.py]
import os
BEETLEJUICE_STATE = os.environ.get('BTJC_STATE',  None)

if BEETLEJUICE_STATE == 'LOCAL':
    from local_settings import *
elif BEETLEJUICE_STATE == 'DEV':
    from development_settings import *
elif BEETLEJUICE_STATE == 'PROD':
    from production_settings import *
else:
    raise ImportError

<aside>
... which used to be the right way to do things (or so I was told). I'm planning to switch over to the way that Audrey and Danny recommend in Two Scoops of Django, the gist of which being:

settings/
    __init__.py
    base.py
    local.py
    test.py
    production.py

... and explicitly call my settings files from now on, e.g.
python manage.py runserver --settings='settings.production'
...which imports from base.py and has all the production-specific settings.
</aside>

... and I had BTJC_STATE being exported in the virtualenvwrapper postactivate file, except that by running celeryd via supervisor I wasn't activating anything and thereby skipping that file entirely! This raised the ImportError but I didn't realise that was where it was coming from =(

Adding a string explaining the exception is a good idea... and apparently using ImproperlyConfigured is a better way to raise for this issue. So by changing the one line below in my .conf file, it started working perfectly.

[program:celery_beetlejuice]
<snip>
environment=BTJC_STATE='LOCAL'
<snip>

So that works now and I'm happy, but I need to find a good way of setting environment variables (like database passwords, api keys, and such) that will work well with for inside a non-activated in-use virtualenv.

All advice welcome on that, message me and I'll update this with the solution =)

Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top