Get absolute path of django app

Python modules (including Django apps) have a __file__ attribute that tells you the location of their file on the filesystem, so

import appname
pth = os.path.dirname(appname.__file__)

should do what you want.

In usual circumstances, os.path.absname(appname.__path__[0]), but it’s possible for apps to change that if they want to import files in a weird way.

(I do always do PROJECT_ROOT = os.path.dirname(os.path.abspath(__file__)) in my, though — makes it easy for the various settings that need to be absolute paths.)

Leave a Comment