Skip to main content

A sample settings for your any project. The packages allows to keep project settings in a simple python module. The module path can be specified in environment variables or specified in the method configure.

In addition, environment variables can contain any variables that will not be overwritten by the module. All variables must begin with the base name of the settings and in the upper case.

A project can have multiple settings modules:

  • base - base settings for the project;

  • extra - extra variables, for example dev or staging;

  • global - global variables, like variables by default.

All variables in all modules should be in upper case.

Extra variables declared in the base settings module as settings.EXTRA_VAR = "VAR_VALUE" and use like so VAR = settings.EXTRA_VAR.split('_') + ['example']. In the extra settings module declared extra value of variables as settings.EXTRA_VAR = "EXTRA_VAR_VALUE" and last line import base settings module from my_project.conf.base import *. In result assert VAR == ['EXTRA', 'VAR', 'VALUE', 'example'].

Example

./start.py:

import os

if __name__ == '__main__':
    from my_project import main

    os.environ.setdefault("BASE_SETTINGS_NAME", "MY_PROJECT")
    os.environ.setdefault("MY_PROJECT_SETTINGS_MODULE", "my_project.conf.extra")
    os.environ.setdefault("MY_PROJECT_GLOBAL_SETTINGS_MODULE", "my_project.conf.global")

    os.environ["MY_PROJECT_VAR_2"] = "ENV_VAR_2_VALUE"

    main.run()

./my_project/main.py:

from my_project.conf import settings


def run():
    settings.configure()

    # your code
    from my_project import code

./my_project/conf/__init__.py:

from project_settings import Settings, AppsSettings

settings = Settings()
apps_settings = AppsSettings()

./my_project/conf/extra.py:

from my_project.conf import settings

settings.EXTRA_VAR = "EXTRA_VAR_VALUE"

from my_project.conf.base import *

./my_project/conf/base.py:

from my_project.conf import settings

VAR_1 = "VAR_1_VALUE"
VAR_2 = "VAR_2_VALUE"
settings.EXTRA_VAR = "BASE_EXTRA_VAR_VALUE"
VAR_5 = settings.VAR_4 + "_VAR_5_VALUE"

./my_project/conf/global.py:

VAR_1 = "GLOBAL_VAR_1_VALUE"
VAR_2 = "GLOBAL_VAR_2_VALUE"
VAR_3 = "GLOBAL_VAR_3_VALUE"
VAR_4 = "GLOBAL_VAR_4_VALUE"

./my_project/code.py:

from my_project.conf import settings

assert settings.VAR_1 == "VAR_1_VALUE"
assert settings.VAR_2 == "ENV_VAR_2_VALUE"
assert settings.VAR_3 == "GLOBAL_VAR_3_VALUE"
assert settings.VAR_4 == "EXTRA_VAR_VALUE"
assert settings.VAR_5 == "EXTRA_VAR_VALUE_VAR_5_VALUE"

After running python start.py all settings will be generated after executing settings.configure(). After configuration, you can overwrite the values of variables, for example: settings.VAR_1 = “NEW_VALUE_VAR_1”.

Apps settings

For use in the settings, you must specify a variable with list of applications INSTALLED_APPS = [‘my_project.apps.app’] and initialize apps_settings = AppsSettings(). Next, you must add the package my_project.apps.app module settings on the inside. The module may contain application variables declared in upper case.

./my_project.apps.app.settings.py:

from project_settings import AppSettings


VAR1 = 'app_var1'
VAR2 = 'app_var2'


class AppSettings(AppSettings):
    name = 'app'

    def ready(self):
        from my_project.conf import settings

        settings.READY_VAR = 'APP_READY_VAR'

Application settings will be available as apps_settings.app.VAR1. Application settings variables can also be declared using the environment variables, if it starts with the application name at the upper case, for example export APP_ENV_VAR=example.

Metadata

Release files for project-settings 1.2.0

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for project-settings 1.2.0
File Size Uploaded
project-settings-1.2.0.tar.gz 4.6 kB Details

Built distribution (wheel)

Table of built distributions (wheels) for project-settings 1.2.0
File Interpreter ABI Platform
project_settings-1.2.0-py2.py3-none-any.whl Python 2, Python 3 none any Details

Total release size: 11.3 kB

Release files / project-settings-1.2.0.tar.gz

Download URL project-settings-1.2.0.tar.gz
Size 4.6 kB
Tags Source
SHA-256 checksum
How to use checksums
a6b0c81486fc6680dad12d87480322d24bf0f3081e6995a27c3cf09a3b53b0df
BLAKE2b-256 checksum
How to use checksums
5555ff692f42815204ad5eb45ba8509a0281e7e29303fb4178c888d1503aa527
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/1.9.1 pkginfo/1.4.1 requests/2.18.4 setuptools/38.5.1 requests-toolbelt/0.8.0 tqdm/4.19.5 CPython/3.5.3

Release files / project_settings-1.2.0-py2.py3-none-any.whl

Download URL project_settings-1.2.0-py2.py3-none-any.whl
Size 6.7 kB
Tags Python 2 Python 3
SHA-256 checksum
How to use checksums
b27a032921ecc0ff5e66449e885a3397ecae4184cb2fc423804adf66bf87cf19
BLAKE2b-256 checksum
How to use checksums
3405f12421573cd20c9301c238489c503068bc8a3cc6527fc7a144b96534bb48
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/1.9.1 pkginfo/1.4.1 requests/2.18.4 setuptools/38.5.1 requests-toolbelt/0.8.0 tqdm/4.19.5 CPython/3.5.3

Release history Release notifications | RSS feed

This release

1.2.0 This release

2 release files

1.1.0

2 release files

1.0.1

2 release files

1.0.0

2 release files

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page