0

I want to implement settings to my python project, Now, the settings.py file holds the hardcoded values of some variables. I need two types of settings. One is Production-based and other one is staging based. How can I implement it in one file?

  • Well, using separate files for settings is a better convention but as you want it in one file, settings can be separated in if else statements while this will be decided via parameter passed in command to run/deploy. for example: define a variable ENVIRONMENT = 'staging'. Then pass its value ('prod' or 'staging') via command. – Azeem Nov 13 '19 at 07:30

1 Answers1

1

in settings.py file:

LOCAL = 'local'
STAGING = 'staging'
PRODUCTION = 'prod'

ENVIRONMENT = LOCAL

BASE_URL = 'hello_local/'

if ENVIRONMENT == STAGING:
   # here set url for staging
   BASE_URL = 'hello_staging/'
elif ENVIRONMENT == PRODUCTION:
   # here set url for prod
   BASE_URL = 'hello_prod/'

while running from bash/terminal

$ export ENVIRONMENT=staging
$ flask run

or if you are using an IDE like PYCHARM, you can set ENVIRONMENT_VARIABLES as:

ENVIRONMENT = 'staging'
Azeem
  • 292
  • 2
  • 13
  • Thanks to @Azeem, If I am using a settings directory and I wanna import modules from settings package, then how can we do? – PRANJITH P Nov 14 '19 at 05:40
  • Are you asking about python's "Import system"? If yes, it's simple: from settings_folder.settings import ENVIRONMENT Otherwise check into this post if it helps you: https://stackoverflow.com/questions/53648900/python-import-package-modules-before-as-well-as-after-setup-py-install – Azeem Nov 15 '19 at 06:53