#13235 closed (wontfix)
Better manage.py: import django after importing settings
| Reported by: | Alexey Kinyov | Owned by: | |
|---|---|---|---|
| Component: | User Experience | Version: | dev |
| Severity: | Keywords: | manage settings | |
| Cc: | alexey.rudy@… | Triage Stage: | Unreviewed |
| Has patch: | yes | Needs documentation: | no |
| Needs tests: | no | Patch needs improvement: | no |
| Easy pickings: | no | UI/UX: | no |
Description
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
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.
#!/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:
from django.core.management import execute_manager
Change History (2)
comment:1 by , 16 years ago
| Resolution: | → wontfix |
|---|---|
| Status: | new → closed |
comment:2 by , 16 years ago
Sometimes it may be necessary to use custom versions of libs for certain project
A solution exists for this. It's called virtualenv.
To my mind, f you need to do sys path modification, the right place to do that isn't settings.py -- it's manage.py itself.
Marking wontfix; if you feel particularly passionate about this, please start a discussion on django-developers.