Opened 5 years ago

Closed 5 years ago

Last modified 5 years ago

#13235 closed (wontfix)

Better manage.py: import django after importing settings

Reported by: rudi Owned by:
Component: User Experience Version: master
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: UI/UX:

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 Changed 5 years ago by russellm

  • Needs documentation unset
  • Needs tests unset
  • Patch needs improvement unset
  • Resolution set to wontfix
  • Status changed from new to closed

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.

comment:2 Changed 5 years ago by Alex

Sometimes it may be necessary to use custom versions of libs for certain project

A solution exists for this. It's called virtualenv.

Note: See TracTickets for help on using tickets.
Back to Top