Running Django Tests with a Precommit Hook - python

I would like to run all my django tests using mercurial's precommit hook. Whenever a test fails the commit will be aborted.
The goal is to block build-breaking commits as often as possible.
edit: Ended up using the external script route. Here is the reletant portion of my hgrc:
[hooks]
precommit = python ./pinax/projects/lgr/manage.py test lgr_photos --verbosity=0 --noinput
commit = hg push
Here is my progress on the hook function:
from os.path import join, dirname
import sys
from django.core.management import call_command
def hook(ui, repo, **kwargs):
project_path = join( dirname(repo.path), 'pinax', 'projects')
sys.path.insert(0, project_path)
from lgr.manage import *
output = call_command('test', verbosity=0, interactive=False)
#ui.warn(output)
What am I doing wrong here?
PS - It's giving a HUGE error traceback, which is included in it's entirety below
jim#ubuntu:~/workspace/lgr$ hg ci -m 'testing hooks'
No username found, using 'jim#ubuntu.ubuntu-domain' instead
error: precommit hook raised an exception: '_demandmod' object is not iterable
** unknown exception encountered, details follow
** report bug details to http://mercurial.selenic.com/bts/
** or mercurial#selenic.com
** Mercurial Distributed SCM (version 1.3.1)
** Extensions loaded:
Traceback (most recent call last):
File "/usr/bin/hg", line 27, in <module>
mercurial.dispatch.run()
File "/usr/lib/pymodules/python2.6/mercurial/dispatch.py", line 16, in run
sys.exit(dispatch(sys.argv[1:]))
File "/usr/lib/pymodules/python2.6/mercurial/dispatch.py", line 27, in dispatch
return _runcatch(u, args)
File "/usr/lib/pymodules/python2.6/mercurial/dispatch.py", line 43, in _runcatch
return _dispatch(ui, args)
File "/usr/lib/pymodules/python2.6/mercurial/dispatch.py", line 449, in _dispatch
return runcommand(lui, repo, cmd, fullargs, ui, options, d)
File "/usr/lib/pymodules/python2.6/mercurial/dispatch.py", line 317, in runcommand
ret = _runcommand(ui, options, cmd, d)
File "/usr/lib/pymodules/python2.6/mercurial/dispatch.py", line 501, in _runcommand
return checkargs()
File "/usr/lib/pymodules/python2.6/mercurial/dispatch.py", line 454, in checkargs
return cmdfunc()
File "/usr/lib/pymodules/python2.6/mercurial/dispatch.py", line 448, in <lambda>
d = lambda: util.checksignature(func)(ui, *args, **cmdoptions)
File "/usr/lib/pymodules/python2.6/mercurial/util.py", line 402, in check
return func(*args, **kwargs)
File "/usr/lib/pymodules/python2.6/mercurial/commands.py", line 667, in commit
node = cmdutil.commit(ui, repo, commitfunc, pats, opts)
File "/usr/lib/pymodules/python2.6/mercurial/cmdutil.py", line 1213, in commit
return commitfunc(ui, repo, message, match(repo, pats, opts), opts)
File "/usr/lib/pymodules/python2.6/mercurial/commands.py", line 665, in commitfunc
editor=e, extra=extra)
File "/usr/lib/pymodules/python2.6/mercurial/localrepo.py", line 886, in commit
ret = self.commitctx(cctx, True)
File "/usr/lib/pymodules/python2.6/mercurial/localrepo.py", line 915, in commitctx
self.hook("precommit", throw=True, parent1=xp1, parent2=xp2)
File "/usr/lib/pymodules/python2.6/mercurial/localrepo.py", line 139, in hook
return hook.hook(self.ui, self, name, throw, **args)
File "/usr/lib/pymodules/python2.6/mercurial/hook.py", line 119, in hook
r = _pythonhook(ui, repo, name, hname, hookfn, args, throw) or r
File "/usr/lib/pymodules/python2.6/mercurial/hook.py", line 52, in _pythonhook
r = obj(ui=ui, repo=repo, hooktype=name, **args)
here is the important part:
File "/home/jim/run_lgr_tests.py", line 11, in hook
output = call_command('test', verbosity=0, interactive=False)
and the rest:
File "/usr/local/lib/python2.6/dist-packages/django/core/management/__init__.py", line 166, in call_command
return klass.execute(*args, **defaults)
File "/usr/local/lib/python2.6/dist-packages/django/core/management/base.py", line 213, in execute
translation.activate('en-us')
File "/usr/local/lib/python2.6/dist-packages/django/utils/translation/__init__.py", line 73, in activate
return real_activate(language)
File "/usr/local/lib/python2.6/dist-packages/django/utils/translation/__init__.py", line 43, in delayed_loader
return g['real_%s' % caller](*args, **kwargs)
File "/usr/local/lib/python2.6/dist-packages/django/utils/translation/trans_real.py", line 205, in activate
_active[currentThread()] = translation(language)
File "/usr/local/lib/python2.6/dist-packages/django/utils/translation/trans_real.py", line 194, in translation
default_translation = _fetch(settings.LANGUAGE_CODE)
File "/usr/local/lib/python2.6/dist-packages/django/utils/translation/trans_real.py", line 180, in _fetch
app = import_module(appname)
File "/usr/local/lib/python2.6/dist-packages/django/utils/importlib.py", line 35, in import_module
__import__(name)
File "/usr/lib/pymodules/python2.6/mercurial/demandimport.py", line 84, in _demandimport
return _origimport(name, globals, locals, fromlist)
File "/home/jim/workspace/lgr/pinax/projects/lgr/apps/lgr_hacks.py", line 5, in <module>
User.email = models.EmailField(_('email address'), blank=True, max_length=200)
File "/usr/local/lib/python2.6/dist-packages/django/utils/translation/__init__.py", line 62, in ugettext
return real_ugettext(message)
File "/usr/local/lib/python2.6/dist-packages/django/utils/translation/trans_real.py", line 286, in ugettext
return do_translate(message, 'ugettext')
File "/usr/local/lib/python2.6/dist-packages/django/utils/translation/trans_real.py", line 276, in do_translate
_default = translation(settings.LANGUAGE_CODE)
File "/usr/local/lib/python2.6/dist-packages/django/utils/translation/trans_real.py", line 194, in translation
default_translation = _fetch(settings.LANGUAGE_CODE)
File "/usr/local/lib/python2.6/dist-packages/django/utils/translation/trans_real.py", line 180, in _fetch
app = import_module(appname)
File "/usr/local/lib/python2.6/dist-packages/django/utils/importlib.py", line 35, in import_module
__import__(name)
File "/usr/lib/pymodules/python2.6/mercurial/demandimport.py", line 84, in _demandimport
return _origimport(name, globals, locals, fromlist)
File "/home/jim/workspace/lgr/pinax/apps/external_apps/djangodblog/__init__.py", line 1, in <module>
import djangodblog.admin
File "/usr/lib/pymodules/python2.6/mercurial/demandimport.py", line 91, in _demandimport
return _origimport(name, globals, locals, fromlist)
File "/home/jim/workspace/lgr/pinax/apps/external_apps/djangodblog/admin.py", line 14, in <module>
admin.site.register(ErrorBatch, ErrorBatchAdmin)
File "/usr/local/lib/python2.6/dist-packages/django/contrib/admin/sites.py", line 90, in register
validate(admin_class, model)
File "/usr/local/lib/python2.6/dist-packages/django/contrib/admin/validation.py", line 22, in validate
models.get_apps()
File "/usr/local/lib/python2.6/dist-packages/django/db/models/loading.py", line 100, in get_apps
self._populate()
File "/usr/local/lib/python2.6/dist-packages/django/db/models/loading.py", line 58, in _populate
self.load_app(app_name, True)
File "/usr/local/lib/python2.6/dist-packages/django/db/models/loading.py", line 74, in load_app
models = import_module('.models', app_name)
File "/usr/local/lib/python2.6/dist-packages/django/utils/importlib.py", line 35, in import_module
__import__(name)
File "/usr/lib/pymodules/python2.6/mercurial/demandimport.py", line 84, in _demandimport
return _origimport(name, globals, locals, fromlist)
File "/home/jim/workspace/lgr/pinax/projects/lgr/apps/account/models.py", line 7, in <module>
from timezones.fields import TimeZoneField
File "/usr/lib/pymodules/python2.6/mercurial/demandimport.py", line 103, in _demandimport
mod = _origimport(name, globals, locals)
File "/home/jim/workspace/lgr/pinax/apps/external_apps/timezones/fields.py", line 12, in <module>
default_tz = pytz.timezone(getattr(settings, "TIME_ZONE", "UTC"))
File "/usr/lib/pymodules/python2.6/mercurial/demandimport.py", line 75, in __getattribute__
self._load()
File "/usr/lib/pymodules/python2.6/mercurial/demandimport.py", line 47, in _load
mod = _origimport(head, globals, locals)
File "/home/jim/workspace/lgr/pinax/libs/external_libs/pytz-2008b/pytz/__init__.py", line 29, in <module>
from pkg_resources import resource_stream
File "/usr/lib/pymodules/python2.6/mercurial/demandimport.py", line 103, in _demandimport
mod = _origimport(name, globals, locals)
File "/usr/lib/python2.6/dist-packages/pkg_resources.py", line 2562, in <module>
working_set.require(__requires__)
File "/usr/lib/python2.6/dist-packages/pkg_resources.py", line 626, in require
needed = self.resolve(parse_requirements(requirements))
File "/usr/lib/python2.6/dist-packages/pkg_resources.py", line 505, in resolve
requirements = list(requirements)[::-1] # set up the stack
File "/usr/lib/python2.6/dist-packages/pkg_resources.py", line 2380, in parse_requirements
for line in lines:
File "/usr/lib/python2.6/dist-packages/pkg_resources.py", line 1814, in yield_lines
for s in yield_lines(ss):
File "/usr/lib/python2.6/dist-packages/pkg_resources.py", line 1813, in yield_lines
for ss in strs:
TypeError: '_demandmod' object is not iterable

It looks like Mercurial's way of importing modules is clashing with Django's somehow.
Before trying to dive in and sort that out, is there any reason to not just run the tests with the normal command?
[hooks]
precommit.runtests = python manage.py test

Related

No module named misc when implementing a simple pytest plugin?

I'm trying to implement the pytest plugin mentioned in https://github.com/pytest-dev/pytest/issues/2039, basically this plugin will print all tests that pytest finds:
## pytest_collector.py
import sys
import pytest
class DkPytestCollector(object):
def __init__(self):
self.collected = []
def pytest_collection_modifyitems(self, items):
for item in items:
self.collected.append(item.nodeid)
if __name__ == '__main__':
dkpytest_collector = DkPytestCollector()
directory = sys.argv[1]
pytest.main(['--collect-only', directory], plugins=[dkpytest_collector])
print('\n\nfound:')
for nodeid in dkpytest_collector.collected:
print(nodeid)
however, when I run it I get:
(dk) go|c:\srv\lib\dk> python ..\dkbuild\dkbuild\pytest_collector.py \srv\lib\dk
Traceback (most recent call last):
File "..\dkbuild\dkbuild\pytest_collector.py", line 20, in <module>
pytest.main(['--collect-only', directory], plugins=[dkpytest_collector])
File "c:\srv\venv\dk\lib\site-packages\_pytest\config.py", line 50, in main
config = _prepareconfig(args, plugins)
File "c:\srv\venv\dk\lib\site-packages\_pytest\config.py", line 160, in _prepareconfig
pluginmanager=pluginmanager, args=args)
File "c:\srv\venv\dk\lib\site-packages\pluggy\__init__.py", line 617, in __call__
return self._hookexec(self, self._nonwrappers + self._wrappers, kwargs)
File "c:\srv\venv\dk\lib\site-packages\pluggy\__init__.py", line 222, in _hookexec
return self._inner_hookexec(hook, methods, kwargs)
File "c:\srv\venv\dk\lib\site-packages\pluggy\__init__.py", line 216, in <lambda>
firstresult=hook.spec_opts.get('firstresult'),
File "c:\srv\venv\dk\lib\site-packages\pluggy\callers.py", line 196, in _multicall
gen.send(outcome)
File "c:\srv\venv\dk\lib\site-packages\_pytest\helpconfig.py", line 68, in pytest_cmdline_parse
config = outcome.get_result()
File "c:\srv\venv\dk\lib\site-packages\pluggy\callers.py", line 77, in get_result
_reraise(*ex) # noqa
File "c:\srv\venv\dk\lib\site-packages\pluggy\callers.py", line 180, in _multicall
res = hook_impl.function(*args)
File "c:\srv\venv\dk\lib\site-packages\_pytest\config.py", line 943, in pytest_cmdline_parse
self.parse(args)
File "c:\srv\venv\dk\lib\site-packages\_pytest\config.py", line 1108, in parse
self._preparse(args, addopts=addopts)
File "c:\srv\venv\dk\lib\site-packages\_pytest\config.py", line 1071, in _preparse
self.pluginmanager.load_setuptools_entrypoints('pytest11')
File "c:\srv\venv\dk\lib\site-packages\pluggy\__init__.py", line 397, in load_setuptools_entrypoints
plugin = ep.load()
File "c:\srv\venv\dk\lib\site-packages\pkg_resources\__init__.py", line 2405, in load
return self.resolve()
File "c:\srv\venv\dk\lib\site-packages\pkg_resources\__init__.py", line 2411, in resolve
module = __import__(self.module_name, fromlist=['__name__'], level=0)
File "c:\srv\venv\dk\lib\site-packages\_pytest\assertion\rewrite.py", line 212, in load_module
py.builtin.exec_(co, mod.__dict__)
File "c:\srv\venv\dk\lib\site-packages\py\_builtin.py", line 221, in exec_
exec2(obj, globals, locals)
File "<string>", line 7, in exec2
File "c:\srv\venv\dk\lib\site-packages\pytest_cov\plugin.py", line 6, in <module>
from coverage.misc import CoverageException
ImportError: No module named misc
if I pip uninstall pytest-cov everything works (well, except no coverage..)
How do I get this to work without uninstalling pytest-cov?

Django: detect module level db queries

Database queries at modul level (aka import-time) can make trouble in django.
See: https://groups.google.com/forum/#!topic/django-developers/7JwWatLfP44/discussion
Trouble:
The real db gets accessed in tests, not the test-database.
Monkey patching in app-ready signals are too late
...
How can I detect the particular python source line which accesses my database before the app ready signal happens?
I found a solution. It's "dirty", but works.
If you add assert 0 to get_new_connection of django/db/backends/postgresql/base.py
Then you see a very long traceback. It reveals where you do access the database too early.
Of course you should only do this in your development environment.
I guess roughly the same should work for sqlite.
foobar_vums_d#aptguettler:~$ py.test -k ...
Traceback (most recent call last):
File "/home/foobar_vums_d/bin/py.test", line 11, in <module>
sys.exit(main())
File "/home/foobar_vums_d/local/lib/python2.7/site-packages/_pytest/config.py", line 49, in main
return config.hook.pytest_cmdline_main(config=config)
File "/home/foobar_vums_d/local/lib/python2.7/site-packages/_pytest/vendored_packages/pluggy.py", line 724, in __call__
return self._hookexec(self, self._nonwrappers + self._wrappers, kwargs)
File "/home/foobar_vums_d/local/lib/python2.7/site-packages/_pytest/vendored_packages/pluggy.py", line 338, in _hookexec
return self._inner_hookexec(hook, methods, kwargs)
File "/home/foobar_vums_d/local/lib/python2.7/site-packages/_pytest/vendored_packages/pluggy.py", line 333, in <lambda>
_MultiCall(methods, kwargs, hook.spec_opts).execute()
File "/home/foobar_vums_d/local/lib/python2.7/site-packages/_pytest/vendored_packages/pluggy.py", line 596, in execute
res = hook_impl.function(*args)
File "/home/foobar_vums_d/local/lib/python2.7/site-packages/pytest_djangotools/hooks.py", line 46, in pytest_cmdline_main
import consolescript_django
File "/home/foobar_vums_d/src/djangotools/consolescript_django/__init__.py", line 11, in <module>
django.setup()
File "/home/foobar_vums_d/local/lib/python2.7/site-packages/django/__init__.py", line 18, in setup
apps.populate(settings.INSTALLED_APPS)
File "/home/foobar_vums_d/local/lib/python2.7/site-packages/django/apps/registry.py", line 115, in populate
app_config.ready()
File "/home/foobar_vums_d/src/djangotools/djangotools/apps.py", line 19, in ready
url_patterns = get_resolver(None).url_patterns
File "/home/foobar_vums_d/local/lib/python2.7/site-packages/django/utils/functional.py", line 33, in __get__
res = instance.__dict__[self.name] = self.func(instance)
File "/home/foobar_vums_d/local/lib/python2.7/site-packages/django/core/urlresolvers.py", line 417, in url_patterns
patterns = getattr(self.urlconf_module, "urlpatterns", self.urlconf_module)
File "/home/foobar_vums_d/local/lib/python2.7/site-packages/django/utils/functional.py", line 33, in __get__
res = instance.__dict__[self.name] = self.func(instance)
File "/home/foobar_vums_d/local/lib/python2.7/site-packages/django/core/urlresolvers.py", line 410, in urlconf_module
return import_module(self.urlconf_name)
File "/usr/lib/python2.7/importlib/__init__.py", line 37, in import_module
__import__(name)
File "/home/foobar_vums_d/src/foobar-vums/foobar_vums/etc/rooturls.py", line 11, in <module>
urlpatterns = collect_urlpatterns_from_apps()
File "/home/foobar_vums_d/src/djangotools/djangotools/utils/urlresolverutils.py", line 124, in collect_urlpatterns_from_apps
urls_module = importlib.import_module(app_import_string + '.urls')
File "/usr/lib/python2.7/importlib/__init__.py", line 37, in import_module
__import__(name)
File "/home/foobar_vums_d/src/foobar/foobar/urls.py", line 310, in <module>
urlpatterns.append(url(r'^', include(collect_urlpatterns_from_package(views))))
File "/home/foobar_vums_d/src/djangotools/djangotools/utils/urlresolverutils.py", line 94, in collect_urlpatterns_from_package
import_package(views_package)
File "/home/foobar_vums_d/src/djangotools/djangotools/utils/urlresolverutils.py", line 89, in import_package
importlib.import_module(module_name)
File "/usr/lib/python2.7/importlib/__init__.py", line 37, in import_module
__import__(name)
File "/home/foobar_vums_d/src/foobar/foobar/views/issue/legacy_edit2.py", line 55, in <module>
for index in Index.objects.all():
File "/home/foobar_vums_d/local/lib/python2.7/site-packages/django/db/models/query.py", line 258, in __iter__
self._fetch_all()
File "/home/foobar_vums_d/local/lib/python2.7/site-packages/django/db/models/query.py", line 1074, in _fetch_all
self._result_cache = list(self.iterator())
File "/home/foobar_vums_d/local/lib/python2.7/site-packages/django/db/models/query.py", line 52, in __iter__
results = compiler.execute_sql()
File "/home/foobar_vums_d/local/lib/python2.7/site-packages/django/db/models/sql/compiler.py", line 846, in execute_sql
cursor = self.connection.cursor()
File "/home/foobar_vums_d/local/lib/python2.7/site-packages/django/db/backends/base/base.py", line 231, in cursor
cursor = self.make_debug_cursor(self._cursor())
File "/home/foobar_vums_d/local/lib/python2.7/site-packages/django/db/backends/base/base.py", line 204, in _cursor
self.ensure_connection()
File "/home/foobar_vums_d/local/lib/python2.7/site-packages/django/db/backends/base/base.py", line 199, in ensure_connection
self.connect()
File "/home/foobar_vums_d/local/lib/python2.7/site-packages/django/db/backends/base/base.py", line 171, in connect
self.connection = self.get_new_connection(conn_params)
File "/home/foobar_vums_d/local/lib/python2.7/site-packages/django/db/backends/postgresql/base.py", line 175, in get_new_connection
connection = Database.connect(**conn_params)
File "/usr/lib/python2.7/dist-packages/psycopg2/__init__.py", line 105, in connect
assert 0
AssertionError
In this case the buggy line is in src/foobar/foobar/views/issue/legacy_edit2.py
You find this by going the stack from bottom to top, skipping all lines containing /site-packages/django/.

Django manage.py runserver got AppRegistryNotReady: Models aren't loaded yet

I hit a problem when run django from command line with manage.py runserver.
The same code is fine with Django 1.5 several months ago.
Today I wanna pickup the code again and run upon Django 1.8.3 and python2.7.10 .
Now, got error here:
Traceback (most recent call last):
File "manage.py", line 29, in <module>
execute_from_command_line(sys.argv)
File "/usr/local/lib/python2.7/dist-packages/django/core/management/__init__.py", line 338, in execute_from_command_line
utility.execute()
File "/usr/local/lib/python2.7/dist-packages/django/core/management/__init__.py", line 312, in execute
django.setup()
File "/usr/local/lib/python2.7/dist-packages/django/__init__.py", line 18, in setup
apps.populate(settings.INSTALLED_APPS)
File "/usr/local/lib/python2.7/dist-packages/django/apps/registry.py", line 108, in populate
app_config.import_models(all_models)
File "/usr/local/lib/python2.7/dist-packages/django/apps/config.py", line 198, in import_models
self.models_module = import_module(models_module_name)
File "/usr/lib/python2.7/importlib/__init__.py", line 37, in import_module
__import__(name)
File "/media/wni/study/workspace4320151111/weichun/mytheme/models.py", line 8, in <module>
from mezzanine.pages.models import Page
File "/media/wni/study/workspace4320151111/weichun/mezzanine/pages/models.py", line 34, in <module>
class Page(BasePage):
File "/media/wni/study/workspace4320151111/weichun/mezzanine/core/models.py", line 350, in __new__
return super(OrderableBase, cls).__new__(cls, name, bases, attrs)
File "/usr/local/lib/python2.7/dist-packages/django/db/models/base.py", line 298, in __new__
new_class.add_to_class(field.name, copy.deepcopy(field))
File "/usr/local/lib/python2.7/dist-packages/django/db/models/base.py", line 324, in add_to_class
value.contribute_to_class(cls, name)
File "/media/wni/study/workspace4320151111/weichun/mezzanine/generic/fields.py", line 226, in contribute_to_class
super(KeywordsField, self).contribute_to_class(cls, name)
File "/media/wni/study/workspace4320151111/weichun/mezzanine/generic/fields.py", line 84, in contribute_to_class
cls._meta.get_fields_with_model()]:
File "/usr/local/lib/python2.7/dist-packages/django/db/models/options.py", line 56, in wrapper
return fn(*args, **kwargs)
File "/usr/local/lib/python2.7/dist-packages/django/db/models/options.py", line 432, in get_fields_with_model
return [self._map_model(f) for f in self.get_fields()]
File "/usr/local/lib/python2.7/dist-packages/django/db/models/options.py", line 740, in get_fields
return self._get_fields(include_parents=include_parents, include_hidden=include_hidden)
File "/usr/local/lib/python2.7/dist-packages/django/db/models/options.py", line 802, in _get_fields
all_fields = self._relation_tree
File "/usr/local/lib/python2.7/dist-packages/django/utils/functional.py", line 60, in __get__
res = instance.__dict__[self.name] = self.func(instance)
File "/usr/local/lib/python2.7/dist-packages/django/db/models/options.py", line 709, in _relation_tree
return self._populate_directed_relation_graph()
File "/usr/local/lib/python2.7/dist-packages/django/db/models/options.py", line 681, in _populate_directed_relation_graph
all_models = self.apps.get_models(include_auto_created=True)
File "/usr/local/lib/python2.7/dist-packages/django/utils/lru_cache.py", line 101, in wrapper
result = user_function(*args, **kwds)
File "/usr/local/lib/python2.7/dist-packages/django/apps/registry.py", line 168, in get_models
self.check_models_ready()
File "/usr/local/lib/python2.7/dist-packages/django/apps/registry.py", line 131, in check_models_ready
raise AppRegistryNotReady("Models aren't loaded yet.")
django.core.exceptions.AppRegistryNotReady: Models aren't loaded yet.
Anyone knows how to fix it?
Thanks.
Wesley
I think you need to change your wsgi.py file as you go for different version of Django.
import os
import sys
from django.core.handlers.wsgi import WSGIHandler
os.environ['DJANGO_SETTINGS_MODULE'] = 'YourAppName.settings'
application = WSGIHandler()
And try to comment all third party application imported in settings.py.
1] ./manage.py runserver will use your wsgi.py however it looks like the stack trace you've shown at the top does not include the wsgi file. Therefore the error is occurring before the wsgi file is loaded.
2] This could well be an issue with your Django settings. For example,may in LOGGING a filename in a non-existent directory.
3] or check this

Django/PostgreSQL - ImproperlyConfigured Error

I started a Django project using PostgreSQL and it was working fine after setup, I could add things to the database, view it in admin etc. Being new to Postgres, I thought it would be a good idea to quit the terminal to see if if it would 'auto connect' to the database upon restarting (like SQLite does). However now it throws this error:
Kyle$ python manage.py runserver
Unhandled exception in thread started by <function wrapper at 0x10f6e4c08>
Traceback (most recent call last):
File "/Library/Python/2.7/site-packages/django/utils/autoreload.py", line 226, in wrapper
fn(*args, **kwargs)
File "/Library/Python/2.7/site-packages/django/core/management/commands/runserver.py", line 109, in inner_run
autoreload.raise_last_exception()
File "/Library/Python/2.7/site-packages/django/utils/autoreload.py", line 249, in raise_last_exception
six.reraise(*_exception)
File "/Library/Python/2.7/site-packages/django/utils/autoreload.py", line 226, in wrapper
fn(*args, **kwargs)
File "/Library/Python/2.7/site-packages/django/__init__.py", line 18, in setup
apps.populate(settings.INSTALLED_APPS)
File "/Library/Python/2.7/site-packages/django/apps/registry.py", line 108, in populate
app_config.import_models(all_models)
File "/Library/Python/2.7/site-packages/django/apps/config.py", line 202, in import_models
self.models_module = import_module(models_module_name)
File "/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/importlib/__init__.py", line 37, in import_module
__import__(name)
File "/Library/Python/2.7/site-packages/django/contrib/auth/models.py", line 4, in <module>
from django.contrib.auth.base_user import AbstractBaseUser, BaseUserManager
File "/Library/Python/2.7/site-packages/django/contrib/auth/base_user.py", line 49, in <module>
class AbstractBaseUser(models.Model):
File "/Library/Python/2.7/site-packages/django/db/models/base.py", line 108, in __new__
new_class.add_to_class('_meta', Options(meta, app_label))
File "/Library/Python/2.7/site-packages/django/db/models/base.py", line 299, in add_to_class
value.contribute_to_class(cls, name)
File "/Library/Python/2.7/site-packages/django/db/models/options.py", line 263, in contribute_to_class
self.db_table = truncate_name(self.db_table, connection.ops.max_name_length())
File "/Library/Python/2.7/site-packages/django/db/__init__.py", line 36, in __getattr__
return getattr(connections[DEFAULT_DB_ALIAS], item)
File "/Library/Python/2.7/site-packages/django/db/utils.py", line 212, in __getitem__
backend = load_backend(db['ENGINE'])
File "/Library/Python/2.7/site-packages/django/db/utils.py", line 116, in load_backend
return import_module('%s.base' % backend_name)
File "/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/importlib/__init__.py", line 37, in import_module
__import__(name)
File "/Library/Python/2.7/site-packages/django/db/backends/postgresql/base.py", line 24, in <module>
raise ImproperlyConfigured("Error loading psycopg2 module: %s" % e)
django.core.exceptions.ImproperlyConfigured: Error loading psycopg2 module: No module named psycopg2
Unhandled exception in thread started by <function wrapper at 0x10b03bc08>
Traceback (most recent call last):
File "/Library/Python/2.7/site-packages/django/utils/autoreload.py", line 226, in wrapper
fn(*args, **kwargs)
File "/Library/Python/2.7/site-packages/django/core/management/commands/runserver.py", line 109, in inner_run
autoreload.raise_last_exception()
File "/Library/Python/2.7/site-packages/django/utils/autoreload.py", line 249, in raise_last_exception
six.reraise(*_exception)
File "/Library/Python/2.7/site-packages/django/utils/autoreload.py", line 226, in wrapper
fn(*args, **kwargs)
File "/Library/Python/2.7/site-packages/django/__init__.py", line 18, in setup
apps.populate(settings.INSTALLED_APPS)
File "/Library/Python/2.7/site-packages/django/apps/registry.py", line 108, in populate
app_config.import_models(all_models)
File "/Library/Python/2.7/site-packages/django/apps/config.py", line 202, in import_models
self.models_module = import_module(models_module_name)
File "/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/importlib/__init__.py", line 37, in import_module
__import__(name)
File "/Library/Python/2.7/site-packages/django/contrib/auth/models.py", line 4, in <module>
from django.contrib.auth.base_user import AbstractBaseUser, BaseUserManager
File "/Library/Python/2.7/site-packages/django/contrib/auth/base_user.py", line 49, in <module>
class AbstractBaseUser(models.Model):
File "/Library/Python/2.7/site-packages/django/db/models/base.py", line 108, in __new__
new_class.add_to_class('_meta', Options(meta, app_label))
File "/Library/Python/2.7/site-packages/django/db/models/base.py", line 299, in add_to_class
value.contribute_to_class(cls, name)
File "/Library/Python/2.7/site-packages/django/db/models/options.py", line 263, in contribute_to_class
self.db_table = truncate_name(self.db_table, connection.ops.max_name_length())
File "/Library/Python/2.7/site-packages/django/db/__init__.py", line 36, in __getattr__
return getattr(connections[DEFAULT_DB_ALIAS], item)
File "/Library/Python/2.7/site-packages/django/db/utils.py", line 212, in __getitem__
backend = load_backend(db['ENGINE'])
File "/Library/Python/2.7/site-packages/django/db/utils.py", line 116, in load_backend
return import_module('%s.base' % backend_name)
File "/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/importlib/__init__.py", line 37, in import_module
__import__(name)
File "/Library/Python/2.7/site-packages/django/db/backends/postgresql/base.py", line 24, in <module>
raise ImproperlyConfigured("Error loading psycopg2 module: %s" % e)
django.core.exceptions.ImproperlyConfigured: Error loading psycopg2 module: No module named psycopg2
When importing the psycopg2 there is no error and inside the psql shell if I type \c it says I have connected to server 'Kyle' (my server name as specified in my django settings) as User 'Kyle' (also as specified in settings).
NB: Using all the latest releases for everything
I think this is a dependency issue, because I realised I wasn't in my virtual environment (it works when I'm in it).

Django multisite: ValueError: SITE_ID has not been set

I recently got a new computer and have been trying to set my development environment back up, which right now I need to develop a Django project. However, I keep running into this following error whenever I try to do python manage.py runserver, makemigrations, migrate, etc and I don't understand why.
Traceback (most recent call last):
File "manage.py", line 17, in <module>
execute_from_command_line(sys.argv)
File "/usr/local/lib/python2.7/site-packages/django/core/management/__init__.py", line 338, in execute_from_command_line
utility.execute()
File "/usr/local/lib/python2.7/site-packages/django/core/management/__init__.py", line 312, in execute
django.setup()
File "/usr/local/lib/python2.7/site-packages/django/__init__.py", line 18, in setup
apps.populate(settings.INSTALLED_APPS)
File "/usr/local/lib/python2.7/site-packages/django/apps/registry.py", line 115, in populate
app_config.ready()
File "/usr/local/lib/python2.7/site-packages/debug_toolbar/apps.py", line 15, in ready
dt_settings.patch_all()
File "/usr/local/lib/python2.7/site-packages/debug_toolbar/settings.py", line 232, in patch_all
patch_root_urlconf()
File "/usr/local/lib/python2.7/site-packages/debug_toolbar/settings.py", line 220, in patch_root_urlconf
reverse('djdt:render_panel')
File "/usr/local/lib/python2.7/site-packages/django/core/urlresolvers.py", line 550, in reverse
app_list = resolver.app_dict[ns]
File "/usr/local/lib/python2.7/site-packages/django/core/urlresolvers.py", line 352, in app_dict
self._populate()
File "/usr/local/lib/python2.7/site-packages/django/core/urlresolvers.py", line 285, in _populate
for pattern in reversed(self.url_patterns):
File "/usr/local/lib/python2.7/site-packages/django/core/urlresolvers.py", line 402, in url_patterns
patterns = getattr(self.urlconf_module, "urlpatterns", self.urlconf_module)
File "/usr/local/lib/python2.7/site-packages/django/core/urlresolvers.py", line 396, in urlconf_module
self._urlconf_module = import_module(self.urlconf_name)
File "/usr/local/Cellar/python/2.7.10_2/Frameworks/Python.framework/Versions/2.7/lib/python2.7/importlib/__init__.py", line 37, in import_module
__import__(name)
File "/Users/Lily/Sites/StartupTree/StartupTree/urls.py", line 48, in <module>
+ static(settings.STATIC_URL, document_root=settings.STATICFILES_DIRS[0])
File "/usr/local/lib/python2.7/site-packages/django/conf/urls/__init__.py", line 33, in include
urlconf_module = import_module(urlconf_module)
File "/usr/local/Cellar/python/2.7.10_2/Frameworks/Python.framework/Versions/2.7/lib/python2.7/importlib/__init__.py", line 37, in import_module
__import__(name)
File "/Users/Lily/Sites/StartupTree/branch/urls.py", line 2, in <module>
from branch.views import (
File "/Users/Lily/Sites/StartupTree/branch/views/__init__.py", line 15, in <module>
from app.views import home as home_redirect
File "/Users/Lily/Sites/StartupTree/app/views.py", line 5, in <module>
from emailuser.views import user_login as login_redirect
File "/Users/Lily/Sites/StartupTree/emailuser/views/__init__.py", line 32, in <module>
from branch.modelforms import (
File "/Users/Lily/Sites/StartupTree/branch/modelforms.py", line 139, in <module>
class ExperienceForm(forms.ModelForm):
File "/usr/local/lib/python2.7/site-packages/django/forms/models.py", line 285, in __new__
opts.help_texts, opts.error_messages)
File "/usr/local/lib/python2.7/site-packages/django/forms/models.py", line 212, in fields_for_model
formfield = f.formfield(**kwargs)
File "/usr/local/lib/python2.7/site-packages/django/db/models/fields/related.py", line 1981, in formfield
'queryset': self.rel.to._default_manager.using(db),
File "/usr/local/lib/python2.7/site-packages/django/db/models/manager.py", line 127, in manager_method
return getattr(self.get_queryset(), name)(*args, **kwargs)
File "/usr/local/lib/python2.7/site-packages/django/contrib/sites/managers.py", line 66, in get_queryset
**{self._get_field_name() + '__id': settings.SITE_ID})
File "/usr/local/lib/python2.7/site-packages/django/db/models/query.py", line 679, in filter
return self._filter_or_exclude(False, *args, **kwargs)
File "/usr/local/lib/python2.7/site-packages/django/db/models/query.py", line 697, in _filter_or_exclude
clone.query.add_q(Q(*args, **kwargs))
File "/usr/local/lib/python2.7/site-packages/django/db/models/sql/query.py", line 1309, in add_q
clause, require_inner = self._add_q(where_part, self.used_aliases)
File "/usr/local/lib/python2.7/site-packages/django/db/models/sql/query.py", line 1337, in _add_q
allow_joins=allow_joins, split_subq=split_subq,
File "/usr/local/lib/python2.7/site-packages/django/db/models/sql/query.py", line 1208, in build_filter
condition = self.build_lookup(lookups, col, value)
File "/usr/local/lib/python2.7/site-packages/django/db/models/sql/query.py", line 1101, in build_lookup
return final_lookup(lhs, rhs)
File "/usr/local/lib/python2.7/site-packages/django/db/models/lookups.py", line 101, in __init__
self.rhs = self.get_prep_lookup()
File "/usr/local/lib/python2.7/site-packages/django/db/models/lookups.py", line 139, in get_prep_lookup
return self.lhs.output_field.get_prep_lookup(self.lookup_name, self.rhs)
File "/usr/local/lib/python2.7/site-packages/django/db/models/fields/__init__.py", line 727, in get_prep_lookup
return self.get_prep_value(value)
File "/usr/local/lib/python2.7/site-packages/django/db/models/fields/__init__.py", line 985, in get_prep_value
return int(value)
File "/usr/local/lib/python2.7/site-packages/multisite/threadlocals.py", line 57, in __int__
return self.get_default()
File "/usr/local/lib/python2.7/site-packages/multisite/threadlocals.py", line 119, in get_default
raise ValueError('SITE_ID has not been set.')
ValueError: SITE_ID has not been set.
I've checked my settings.py and found SITE_ID = 1 so I'm assuming that's what it wants? (I'm also very new to Django, sorry!)
Thanks so much in advance for your help!
When using django-multisite you need to set up the SITE_ID as follows
from multisite import SiteID
SITE_ID = SiteID()
From command line intro your enviroment:
$ export SITE_ID = 1

Categories