Opened 10 years ago

Last modified 10 years ago

#23436 closed Cleanup/optimization

Should use abspath for default settings.BASE_DIR — at Version 2

Reported by: Harry Percival Owned by: nobody
Component: Core (Other) Version: 1.6
Severity: Normal Keywords:
Cc: Triage Stage: Accepted
Has patch: no Needs documentation: no
Needs tests: no Patch needs improvement: no
Easy pickings: no UI/UX: no

Description (last modified by Harry Percival)

ref the default project template's settings.py, BASE_DIR is defined as:

BASE_DIR = os.path.dirname(os.path.dirname(__file__))

Because there's no abspath, you can get different results for BASE_DIR depending on how settings.py is run. This is an edge case, since settings.py is almost always imported by django in the right context, but still -- compare:

python manage.py shell
>>> from django.conf import settings
print(BASE_DIR)

With:

python -i myproject/settings.py
>>> print(BASE_DIR)

As an aside, in general, in any path wrangling you should always call abspath first. Try running this script, saved to a file:

import os
print(os.path.abspath(os.path.dirname(os.path.dirname(__file__))))
print(os.path.dirname(os.path.dirname(os.path.abspath(__file__))))

As to tests, the only one I can think of would be a pretty horrible one involving actually callling out using subprocess.Popen to a python -i of the project template settings file, so maybe ok not to bother?

credit to @CleanCut for turning me onto this weirdness!

Change History (2)

comment:1 by Harry Percival, 10 years ago

Description: modified (diff)

comment:2 by Harry Percival, 10 years ago

Description: modified (diff)
Note: See TracTickets for help on using tickets.
Back to Top