Skip to main content

CircleCI

python-settings

This module provides you easy access to your config/settings properties from all your python modules, it supports normal and lazy initialization for each property. It is based on django.conf.settings.

Installation

From pip

pip install python-settings

Or

Clone this repo and type

python setup.py install

How to configure

Create a python file like settings.py in your project, the variable names must be in Capital Letters (A-Z), example:

# settings.py

# Variables definition
DATABASE_HOST = '10.0.0.1'

DATABASE_NAME = 'DATABASENAME'

Two optional patterns to initialize this library

  • Option 1. Using an environment variable. You must have an environment variable called SETTINGS_MODULE and as a value your just created python settings file in the format {module}. {name}. With no .py extension.

    Example in bash:

     export SETTINGS_MODULE=settings
    

    Example in Python

    import os
    os.environ["SETTINGS_MODULE"] = 'settings' 
    
  • Option 2. Calling the configure function from our settings module and passing it your python file

    from python_settings import settings
    from . import settings as my_local_settings
    
    settings.configure(my_local_settings) # configure() receives a python module
    assert settings.configured # now you are set
    

How to use

Import the settings module and access directly to your properties:

from python_settings import settings 

print(settings.DATABASE_HOST) # Will print '10.0.0.1'
print(settings.DATABASE_NAME) # Will print 'DATABASENAME'

Lazy Initialization

Every time you start/restart your python project, all your defined variables are evaluated many times, if you are dealing with heavy to instantiate objects like database connections or similar network calls you will expect some delay.

Using Lazy Initialization increases the performance of this process, changing the behavior of evaluating the variables only when is needed.

Use the Lazy Initializer

In your python settings file, you have to import our LazySetting class located in python_settings.

from python_settings import LazySetting
from my_awesome_library import HeavyInitializationClass # Heavy to initialize object

LAZY_INITIALIZATION = LazySetting(HeavyInitializationClass, "127.0.0.1:4222") 
# LazySetting(Class, *args, **kwargs)

Only the first time you call this property, the HeavyInitializationClass will be instantiated and the *args and **kwargs parameters will be passed. Every time you call this property the same instance will be returned.

And now from any place in your code, you have to call the property

from python_settings import settings 

object_initialized = settings.LAZY_INITIALIZATION # Will return an instance of your object

Example for different environments

You can use as many settings files as you need for different environments. Example for development environment settings:

# development_settings.py
import os

from .base_settings import *


TOKEN_API = os.environ.get("TOKEN_API")

Example for testing environment

# testing_settings.py
import os

from .settings import *

DATABASE_HOST = '10.0.0.1'

TOKEN_API = os.environ.get("TOKEN_API")

And update your SETTINGS_MODULE variable

export SETTINGS_MODULE = 'myproject.settings.testing_settings'

or use the config function

TODO LIST:

  • Add function to update default environment variable name

Metadata

Release files for python-settings 0.2.3

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

Source distribution (sdist)

Source distribution for python-settings 0.2.3
File Size Uploaded Provenance Actions
python_settings-0.2.3.tar.gz 7.7 kB Without provenance Details for python_settings-0.2.3.tar.gz

Built distribution (wheel)

Table of built distributions (wheels) for python-settings 0.2.3
File Interpreter ABI Platform Actions
python_settings-0.2.3-py2.py3-none-any.whl Python 2, Python 3 none any Details for python_settings-0.2.3-py2.py3-none-any.whl

Total release size: 15.8 kB

Release files / python_settings-0.2.3.tar.gz

Download URL python_settings-0.2.3.tar.gz
Size 7.7 kB
Tags Source
SHA-256 checksum
How to use checksums
5d0bb4097440924158234e7db6af9fcc6630c0db624758ffa27fbe7c51865576
BLAKE2b-256 checksum
How to use checksums
316f4036bd79b8910a001b1f39280f2b10644dfd2906f44507563353d657cf2b
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/5.1.1 CPython/3.9.6

Release files / python_settings-0.2.3-py2.py3-none-any.whl

Download URL python_settings-0.2.3-py2.py3-none-any.whl
Size 8.1 kB
Tags Python 2 Python 3
SHA-256 checksum
How to use checksums
4887dc00b3fb41b9aed901c0465285a7114d4050bfe156175513626655cfee71
BLAKE2b-256 checksum
How to use checksums
f87fefc7ba7d2403107be23a23bc2c082fe44d29450016445bbf11d4e6f29c73
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/5.1.1 CPython/3.9.6

Security for python-settings 0.2.3

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

No provenance found

None of the files in this release were uploaded with provenance metadata.

A note on package security

PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.

To learn more about protecting yourself from malicious actors, visit our package security documentation.

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