0

myapp我为文件中的应用程序制作了一个数据库路由器/myproject/myapp/routers.py

class ShardingRouter(object):

    def db_for_read(self, model, **hints):
        return 'default'

    def db_for_write(self, model, **hints):
        return 'default'

    def allow_relation(self, obj1, obj2, **hints):
        return None

    def allow_syncdb(self, db, model):
        return None 

settings.py我有:

from django.db import connections
DATABASE_ROUTERS = ['myproject.myapp.routers.ShardingRouter',]

这适用于通过 wsgi 运行的普通应用程序,但我有一个 CLI 脚本/myproject/parser_jobs.py,它以 cron 开头或从 CLI 手动启动:

import os, sys
if __name__ == "__main__":
    os.environ.setdefault("DJANGO_SETTINGS_MODULE", "settings")
...
...
if __name__ == "__main__":
    do_some_long_boring_work()

现在,当我运行此脚本时,出现导入错误:

Traceback (most recent call last):
  File "/hosting/myproject/myproject/parser_jobs.py", line 20, in <module>
    import settings
  File "/hosting/myproject/myproject/settings.py", line 46, in <module>
    from django.db import connections
  File "/usr/local/lib/python2.7/dist-packages/django/db/__init__.py", line 11, in <module>
    if DEFAULT_DB_ALIAS not in settings.DATABASES:
  File "/usr/local/lib/python2.7/dist-packages/django/utils/functional.py", line 184, in inner
    self._setup()
  File "/usr/local/lib/python2.7/dist-packages/django/conf/__init__.py", line 42, in _setup
    self._wrapped = Settings(settings_module)
  File "/usr/local/lib/python2.7/dist-packages/django/conf/__init__.py", line 95, in __init__
    raise ImportError("Could not import settings '%s' (Is it on sys.path?): %s" % (self.SETTINGS_MODULE, e))
ImportError: Could not import settings 'myproject.myproject' (Is it on sys.path?): cannot import name connections

看起来它找到了 settings.py,但是在导入时它失败了from django.db import connections。如果我评论这个字符串,它可以工作,但没有我的数据库路由器:(我可以在using()任何地方添加,但这并不酷。

因此,网站运行良好,但 cli 脚本失败。请帮忙!


更新:/hosting/myproject/myproject/parser_jobs.py 在我添加数据库路由器之前,从 cli 工作得很好

pprintsys.path这个脚本中:

['/hosting/myproject/myproject',
 '/hosting/myproject',
 '/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/local/lib/python2.7/dist-packages',
 '/usr/lib/python2.7/dist-packages',
 '/usr/lib/python2.7/dist-packages/PIL',
 '/usr/lib/python2.7/dist-packages/gst-0.10',
 '/usr/lib/python2.7/dist-packages/gtk-2.0',
 '/usr/lib/python2.7/dist-packages/ubuntu-sso-client',
 '/usr/lib/pymodules/python2.7']

更新:wsgi 脚本,网站可以很好地使用它:

import os
import sys

path = '/hosting/myproject'
if path not in sys.path:
    sys.path.insert(0, path)

os.environ['DJANGO_SETTINGS_MODULE'] = 'myproject.settings'

import django.core.handlers.wsgi
application = django.core.handlers.wsgi.WSGIHandler()
4

1 回答 1

1

settings.py 我的脚本与当我将它从移动/hosting/myproject/myproject/parser_jobs.py/hosting/myproject/parser_jobs.py (django1.4 manage.py 应该在哪里)时它在同一个文件夹中,它变得正常工作。

os.environ.setdefault("DJANGO_SETTINGS_MODULE", "settings")我也改成os.environ.setdefault("DJANGO_SETTINGS_MODULE", "myproject.settings")

现在它起作用了!

在以前的位置parser_jobs.py只能在没有 db 路由器的情况下工作

于 2012-07-30T21:08:05.193 回答