Note that there are some explanatory texts on larger screens.

plurals
  1. POdjango CLI script and database router: cannot import name connections
    primarykey
    data
    text
    <p>I made a database ruter for <code>myapp</code> application in file <code>/myproject/myapp/routers.py</code></p> <pre><code>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 </code></pre> <p>In <code>settings.py</code> I have:</p> <pre><code>from django.db import connections DATABASE_ROUTERS = ['myproject.myapp.routers.ShardingRouter',] </code></pre> <p>This works well for normal application running through wsgi, but I have one CLI script <code>/myproject/parser_jobs.py</code> it is starting with cron or manually from CLI:</p> <pre><code>import os, sys if __name__ == "__main__": os.environ.setdefault("DJANGO_SETTINGS_MODULE", "settings") ... ... if __name__ == "__main__": do_some_long_boring_work() </code></pre> <p>now, when i run this script, I'm getting import error:</p> <pre><code>Traceback (most recent call last): File "/hosting/myproject/myproject/parser_jobs.py", line 20, in &lt;module&gt; import settings File "/hosting/myproject/myproject/settings.py", line 46, in &lt;module&gt; from django.db import connections File "/usr/local/lib/python2.7/dist-packages/django/db/__init__.py", line 11, in &lt;module&gt; 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 </code></pre> <p>Looks like it found settings.py, but while importing it fails on <code>from django.db import connections</code>. If i comment this string, it works, but without my db router:( I can add <code>using()</code> everywhere, but it's not cool.</p> <p>So, website works good, but cli script fails. Please, help!</p> <hr> <p>update: <code>/hosting/myproject/myproject/parser_jobs.py</code> worked good from cli, before I added DB router</p> <p><code>pprint</code> of <code>sys.path</code> in this script:</p> <pre><code>['/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'] </code></pre> <hr> <p>update: wsgi script, website works good with it:</p> <pre><code>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() </code></pre>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload