Opened 7 years ago

Closed 7 years ago

Last modified 7 years ago

#13235 closed (wontfix)

Better import django after importing settings

Reported by: Alexey Kinyov 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:


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

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 imports django first, then imports settings. It seems, the more preferred way is to import settings first.

#!/usr/bin/env python
    import settings # Assumed to be in the same directory.
except ImportError:
    import sys
    sys.stderr.write("Error: Can't find the file '' in the directory containing %r. It appears you've customized things.\nYou'll have to run, passing it your settings module.\n(If the file does indeed exist, it's causing an ImportError somehow.)\n" % __file__)

from import execute_manager
if __name__ == "__main__":

The example above is based on original, but with one line moved from top to bottom:

from import execute_manager

Change History (2)

comment:1 Changed 7 years ago by Russell Keith-Magee

Needs documentation: unset
Needs tests: unset
Patch needs improvement: unset
Resolution: wontfix
Status: newclosed

To my mind, f you need to do sys path modification, the right place to do that isn't -- it's itself.

Marking wontfix; if you feel particularly passionate about this, please start a discussion on django-developers.

comment:2 Changed 7 years ago by Alex Gaynor

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