django CLI script and database router: cannot import name connections
Question: I made a database ruter for `myapp` application in file
`/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 
    

In `settings.py` I have:

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

This works well for normal application running through wsgi, but I have one
CLI script `/myproject/parser_jobs.py` it is starting with cron or manually
from CLI:

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

now, when i run this script, I'm getting import error:

    
    
    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
    

Looks like it found settings.py, but while importing it fails on `from
django.db import connections`. If i comment this string, it works, but without
my db router:( I can add `using()` everywhere, but it's not cool.

So, website works good, but cli script fails. Please, help!

* * *

update: `/hosting/myproject/myproject/parser_jobs.py` worked good from cli,
before I added DB router

`pprint` of `sys.path` in this script:

    
    
    ['/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']
    

* * *

update: wsgi script, website works good with it:

    
    
    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()
    


Answer: my script was in the same folder with `settings.py` when I moved it from
`/hosting/myproject/myproject/parser_jobs.py` to
`/hosting/myproject/parser_jobs.py` (where in django1.4 manage.py should be)
it become working correct.

Also I changed `os.environ.setdefault("DJANGO_SETTINGS_MODULE", "settings")`
to `os.environ.setdefault("DJANGO_SETTINGS_MODULE", "myproject.settings")`

And now it works!

In previous location `parser_jobs.py` works only without db router

