oz123
oz123

Reputation: 28868

Django's find the path to setting.py

Until not long ago, I used to do this in many places in my Django app:

from  MyApp import settings

This required that I put settings inside my MyApp directory. I realized that is wrong, so I started using:

from django.conf import settings

But now I can't figure out how to find the path to settings.py from withing my code. Earlier I could use settings.__file__.

As a workaround I defined inside settings.py:

PATHTOSELF = os.path.dirname(__file__)

Any ideas how to find the path to settings.py?

Upvotes: 0

Views: 411

Answers (2)

Simon Steinberger
Simon Steinberger

Reputation: 6825

This should work for you:

import os
PROJECT_ROOT = os.path.abspath(os.path.dirname(__file__))

Place it on top inside of your settings.py. You can then import PROJECT_ROOT at any point in your code like this:

from myapp.settings import PROJECT_ROOT

Upvotes: 1

Shashi
Shashi

Reputation: 2175

Try This:

import os
PROJECT_PATH = os.path.dirname(os.path.abspath(__file__))

Upvotes: 2

Related Questions