65π
Python modules (including Django apps) have a __file__
attribute that tells you the location of their __init__.py
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 settings.py
, though β makes it easy for the various settings that need to be absolute paths.)
20π
So the accepted answer usually works fine. However, for
- namespace packages with multiple paths, or
- apps which explicitly configure their paths in the config,
their intended path may not agree with the __file__
attribute of the module.
Django (1.7+) provides the AppConfig.path
attribute β which I think is clearer even in simple cases, and which covers these edge cases too.
The application docs tell you how to get the AppConfig object. So to get AppConfig and print the path from it:
from django.apps import apps
print(apps.get_app_config('app_label').path)
Edit:
Altered example for how to use get_app_config
to remove dots, which seems to have been confusing. Here are the docs for reference.
- [Django]-Django: ImproperlyConfigured: The SECRET_KEY setting must not be empty
- [Django]-413 Request Entity Too Large nginx django
- [Django]-Custom QuerySet and Manager without breaking DRY?
15π
Normally, this is what I add in my settings.py file so I can reference the project root.
import os.path
PROJECT_ROOT = os.path.abspath(os.path.dirname(__file__))
This method will get the directory of any python file.
- [Django]-Can I make list_filter in django admin to only show referenced ForeignKeys?
- [Django]-Django: How to get language code in template?
- [Django]-Django Rest-Framework nested serializer order
5π
Γn newer versions of Django (I donβt know when it started, I assume itβs there for many years now), the default settingy.py contains an entry
# Build paths inside the project like this: os.path.join(BASE_DIR, ...)
BASE_DIR = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
which you can use by importing the settings file like this
import os
from my_project_name.settings import BASE_DIR
os.path.join(BASE_DIR, ...)
- [Django]-ValueError: Related model u'app.model' cannot be resolved
- [Django]-Auto-create primary key used when not defining a primary key type warning in Django
- [Django]-Django 1.8 Run a specific migration
1π
Python3.4 and above comes with standard library pathlib
.
from pathlib import Path
import appmodule
pth = Path(appmodule.__file__).parent / 'fixtures'
if pth.exists():
"Your code here"
parent
will give you the path of your app directory, and /
will append fixtures as path.
- [Django]-Why does Django's render() function need the "request" argument?
- [Django]-Django query negation
- [Django]-Additional field while serializing django rest framework
0π
Keep in mind that appname.__path__
is a list:
import appname
APP_ROOT = os.path.abspath(appname.__path__[0])
file_path = os.path.join(APP_ROOT, "some_file.txt")
- [Django]-Using Basic HTTP access authentication in Django testing framework
- [Django]-What is the difference between static files and media files in Django?
- [Django]-How to get form fields' id in Django
0π
Django auto-generated setting file has the following that you can use as well
BASE_DIR = Path(file).resolve().parent.parent
- [Django]-Get distinct values of Queryset by field
- [Django]-Django β Login with Email
- [Django]-How to show a PDF file in a Django view?