3

从 django 1.2.7 升级到 1.5.1 后尝试使用运行 celery

python manage.py celeryd -v 2 -l INFO --settings=settings

我有一个错误说

django.core.management.execute_manager

在 django 1.4 中已弃用

我的管理.py

#!/usr/bin/env python
from django.core.management import execute_manager
try:
    import settings # Assumed to be in the same directory.
except ImportError:
    import sys
    sys.stderr.write("Error: Can't find the file 'settings.py' in the directory containing %r. It appears you've customized things.\nYou'll have to run django-admin.py, passing it your settings module.\n(If the file settings.py does indeed exist, it's causing an ImportError somehow.)\n" % __file__)
    sys.exit(1)

if __name__ == "__main__":
    execute_from_command_line(settings)

我查看了 django 1.4 发行说明

django-core-management-execute-manager

正如它所说,我用 execute_from_command_line 替换了 execute_manager

现在我在重新启动服务器时开始收到此错误消息

Traceback (most recent call last):
  File "C:\my\manage.py", line 12, in <module>
    execute_from_command_line(settings)
  File "C:\Python27\lib\site-packages\django\core\management\__init__.py", line 452, in     execute_from_command_line
    utility = ManagementUtility(argv)
  File "C:\Python27\lib\site-packages\django\core\management\__init__.py", line 226, in __init__
    self.prog_name = os.path.basename(self.argv[0])
TypeError: 'module' object is not subscriptable
4

1 回答 1

3

您向该execute_from_command_line方法传递了错误的参数。您应该执行以下操作:

#!/usr/bin/env python
import os
import sys

if __name__ == "__main__":
    os.environ.setdefault("DJANGO_SETTINGS_MODULE", "<package>.<subpackage>.settings") #path to the settings py file
    from django.core.management import execute_from_command_line
    execute_from_command_line(sys.argv)

或者

#!/usr/bin/env python
import os

if __name__ == "__main__":
    os.environ.setdefault("DJANGO_SETTINGS_MODULE", "<package>.<subpackage>.settings") #path to the settings py file
    from django.core.management import execute_from_command_line
    execute_from_command_line() # by default sys.argv argument is taken

实际上,execute_from_command_line正如它的名字所暗示的那样,参数是一个解析的命令行,其中第一个元素是可执行文件名称,其他元素是参数

于 2013-07-22T13:23:46.507 回答