﻿id	summary	reporter	owner	description	type	status	component	version	severity	resolution	keywords	cc	stage	has_patch	needs_docs	needs_tests	needs_better_patch	easy	ui_ux
13235	Better manage.py: import django after importing settings	Alexey Kinyov		"Sometimes it may be necessary to use custom versions of libs for certain project. They may be placed to special dir which should be first in `sys.path`. This dir may be inserted to `sys.path` at first lines in settings.py

{{{
#!python
import os
import sys
PROJECT_ROOT = os.path.realpath(os.path.dirname(__file__))
PROJECT_LIBS = os.path.realpath(os.path.join(PROJECT_ROOT, '..', 'lib'))
if not PROJECT_LIBS in sys.path: sys.path.insert(0, PROJECT_LIBS)
}}}

But! We can't place custom `django` to that dir, because `manage.py` imports `django` first, then imports `settings`. It seems, the more preferred way is to import settings first.

{{{
#!python
#!/usr/bin/env python
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)

from django.core.management import execute_manager
if __name__ == ""__main__"":
    execute_manager(settings)
}}}

The example above is based on original manage.py, but with one line moved from top to bottom:
{{{
#!python
from django.core.management import execute_manager
}}}"		closed	User Experience	dev		wontfix	manage settings	alexey.rudy@…	Unreviewed	1	0	0	0	0	0
