为什么要添加导出DJANGO_SETTINGS_MODULE =' myproject.settings'到bashrc不再有效?

时间:2014-08-17 20:06:19

标签: linux django celery django-celery

我在Linux机器上运行了一个Django应用程序(Debian)。在过去的一段时间里,应用程序运行良好。最近我需要在芹菜任务开始挂起后重启机器并清除任务没有达到预期的效果。当我现在尝试使用

开始芹菜
sudo celery -A myapp.tasks worker -Ofair 

我看到了以下追溯

Traceback (most recent call last):
  File "/usr/local/bin/celery", line 9, in <module>
    load_entry_point('celery==3.1.11', 'console_scripts', 'celery')()
  File "/usr/local/lib/python2.7/dist-packages/celery/__main__.py", line 30, in main
    main()
  File "/usr/local/lib/python2.7/dist-packages/celery/bin/celery.py", line 81, in main
    cmd.execute_from_commandline(argv)
  File "/usr/local/lib/python2.7/dist-packages/celery/bin/celery.py", line 769, in execute_from_commandline
    super(CeleryCommand, self).execute_from_commandline(argv)))
  File "/usr/local/lib/python2.7/dist-packages/celery/bin/base.py", line 304, in execute_from_commandline
    argv = self.setup_app_from_commandline(argv)
  File "/usr/local/lib/python2.7/dist-packages/celery/bin/base.py", line 464, in setup_app_from_commandline
    self.app = self.find_app(app)
  File "/usr/local/lib/python2.7/dist-packages/celery/bin/base.py", line 484, in find_app
    return find_app(app, symbol_by_name=self.symbol_by_name)
  File "/usr/local/lib/python2.7/dist-packages/celery/app/utils.py", line 225, in find_app
    sym = imp(app)
  File "/usr/local/lib/python2.7/dist-packages/celery/utils/imports.py", line 101, in import_from_cwd
    return imp(module, package=package)
  File "/usr/lib/python2.7/importlib/__init__.py", line 37, in import_module
    __import__(name)
  File "/home/this/gce_app/myapp/tasks.py", line 8, in <module>
    from django.views.decorators.csrf import csrf_exempt
  File "/usr/local/lib/python2.7/dist-packages/django/views/decorators/csrf.py", line 1, in <module>
    from django.middleware.csrf import CsrfViewMiddleware, get_token
  File "/usr/local/lib/python2.7/dist-packages/django/middleware/csrf.py", line 14, in <module>
    from django.utils.cache import patch_vary_headers
  File "/usr/local/lib/python2.7/dist-packages/django/utils/cache.py", line 26, in <module>
    from django.core.cache import get_cache
  File "/usr/local/lib/python2.7/dist-packages/django/core/cache/__init__.py", line 69, in <module>
    if DEFAULT_CACHE_ALIAS not in settings.CACHES:
  File "/usr/local/lib/python2.7/dist-packages/django/conf/__init__.py", line 54, in __getattr__
    self._setup(name)
  File "/usr/local/lib/python2.7/dist-packages/django/conf/__init__.py", line 47, in _setup
    % (desc, ENVIRONMENT_VARIABLE))
django.core.exceptions.ImproperlyConfigured: Requested setting CACHES, but settings are not configured. You must
 either define the environment variable DJANGO_SETTINGS_MODULE or call settings.configure() before accessing set
tings.

我已将导出添加到bashrc(最后),以解决过去的问题,但现在我仍然遇到了同样的错误:

sudo nano ~/.bashrc
export DJANGO_SETTINGS_MODULE='myproject.settings'

有没有办法让这个再次起作用?

1 个答案:

答案 0 :(得分:5)

您正在使用sudo启动芹菜,这意味着您以root用户身份运行,而不是您为其设置环境变量的用户。不要那样做 - 用sudo运行守护进程绝对没有理由。

(与您的问题无关,但您也不应该将bashrc编辑为sudo。)