0

For example, when using django I can get my settings from anywhere within the project using from djanog.conf import settings. I don't have to specify where django.conf is, it just knows. How does it know?

I ask because I'm building a project where I need to be able to import a conf file without knowing the relative path. Right now I have to figure out the relative path to the conf file each time I use it. I would prefer to just do something like from config import settings. I figure I need to add my conf file to sys.path but I'm not sure where to do this. Is there a standard way for handling this type of problem in python?

I'm sure some variation of this question has been asked hundreds of times before but I can't seem to find the answer. If you know of a good answer that already exists please let me knows. Thanks.

Dallin Davis
  • 421
  • 7
  • 18
  • 1
    Python uses [`sys.path`](https://docs.python.org/3/library/sys.html#sys.path) to determine the default search path for modules. However, please elaborate on your "need to be able to import a conf file without knowing the relative path", please? – AKX May 26 '21 at 17:52
  • Tried to elaborate a bit more. Not completely sure how to elaborate more. – Dallin Davis May 26 '21 at 19:11
  • You should probably look at what `django.conf.settings` does - it's basically a proxy for the user settings module specified by an environment variable. – AKX May 26 '21 at 19:27
  • Perhaps you are thinking `conf` is a file extension, part of the file `django.conf`? That is incorrect, `conf` is a Python module that’s part of the `django` package. As such it is `django` that is found on `sys.path`, and `conf` and `settings` are found by traversing the `django/` directory contents. – Martijn Pieters Jun 05 '21 at 11:31
  • There are too many possibilities for what you are actually trying to do. If you need to find a file relative to your code, see https://stackoverflow.com/questions/10174211/how-to-make-an-always-relative-to-current-module-file-path – Martijn Pieters Jun 05 '21 at 11:51

1 Answers1

1

Well the reason that from django.conf import settings works is that django.conf is just a sub package of the django python package and not a .conf file. So when you do from django.conf import settings the same magic that makes import django work. It is just the standard python import mechanism.

The settings object you get by doing from django.conf import settings is this python object. So you could just try to see what LazySettings() does. The way django actually reads your settings (and not just the global default settings) is by reading the location of the settings file through from environment variable by doing os.environ.get("DJANGO_SETTINGS_MODULE"), and being able to locate the location of the user specified settings file through an environment variable is a good standard practice.

However this does not solve the problem of having a default.conf file somewhere in your project if a user does not specify its own settings file through an environment variable.

For example if you have settings.py and default.conf in the same directory you could do something like

import pathlib
  
DEFAULT_CONF_FILE = pathlib.Path(__file__).parent.joinpath("default.conf").absolute()

in settings.py and then if you need to have the location of default.conf anywhere you can just import DEFAULT_CONF_FILE from settings.py

Maarten Derickx
  • 1,502
  • 1
  • 16
  • 27