Last active
July 18, 2025 18:46
-
-
Save mkpoli/d1b4e9710e763d52e00261d009108cd7 to your computer and use it in GitHub Desktop.
Python Deployment Environment (prod, dev, stage) from Environmental Variables Snippet
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| import os | |
| from enum import Enum | |
| # Change the name of environment variable to be read here | |
| DEPLOY_ENV_VAR_NAME = 'DEPLOY_ENV' | |
| class DeploymentEnvironment(Enum): | |
| PRODUCTION = 'prod' | |
| DEVELOPMENT = 'dev' | |
| STAGING = 'stage' | |
| def __str__(self) -> str: | |
| return str(self.value) | |
| DEPLOYMENT_ENVIRONMENT_REPRESENTATIONS = { | |
| DeploymentEnvironment.DEVELOPMENT: {'dev', 'development', 'trunk'}, | |
| DeploymentEnvironment.STAGING: {'stage','staging', 'demo'}, | |
| DeploymentEnvironment.PRODUCTION: {'prod','production', 'live'}, | |
| } | |
| DEPLOY_ENV_VAR_VALUE = os.environ.get('DEPLOY_ENV', default='').strip().lower() | |
| DEPLOYMENT_ENVIRONMENT = None | |
| for deploy_env in DeploymentEnvironment: | |
| if DEPLOY_ENV_VAR_VALUE in DEPLOYMENT_ENVIRONMENT_REPRESENTATIONS[deploy_env]: | |
| DEPLOYMENT_ENVIRONMENT = deploy_env | |
| break | |
| else: | |
| DEPLOYMENT_ENVIRONMENT = DeploymentEnvironment.DEVELOPMENT | |
| # Check if in specified mode (bools) | |
| DEVELOPMENT_MODE = DEPLOYMENT_ENVIRONMENT is DeploymentEnvironment.DEVELOPMENT | |
| PRODUCTION_MODE = DEPLOYMENT_ENVIRONMENT is DeploymentEnvironment.PRODUCTION | |
| STAGING_MODE = DEPLOYMENT_ENVIRONMENT is DeploymentEnvironment.STAGING |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| { | |
| "version": "0.2.0", | |
| "configurations": [ | |
| { | |
| "name": "Python (dev)", | |
| "type": "python", | |
| "request": "launch", | |
| "program": "usage.py", | |
| "console": "integratedTerminal", | |
| "env": { | |
| "DEPLOY_ENV": "dev" | |
| } | |
| }, { | |
| "name": "Python (prod)", | |
| "type": "python", | |
| "request": "launch", | |
| "program": "usage.py", | |
| "console": "integratedTerminal", | |
| "env": { | |
| "DEPLOY_ENV": "prod" | |
| } | |
| }, { | |
| "name": "Python (stage)", | |
| "type": "python", | |
| "request": "launch", | |
| "program": "usage.py", | |
| "console": "integratedTerminal", | |
| "env": { | |
| "DEPLOY_ENV": "stage" | |
| } | |
| } | |
| ] | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| # Usage 0 | |
| from __environ__ import DEPLOYMENT_ENVIRONMENT | |
| print("Current Deployment Environment is:", DEPLOYMENT_ENVIRONMENT) | |
| # Usage 1 | |
| from __environ__ import DeploymentEnvironment, DEPLOYMENT_ENVIRONMENT | |
| if DEPLOYMENT_ENVIRONMENT is DeploymentEnvironment.PRODUCTION: | |
| # Do something productive! | |
| print("I'm in production mode! Yay!") | |
| else: | |
| print("I'm not in production mode :(") | |
| # Usage 2 | |
| from __environ__ import DEVELOPMENT_MODE | |
| if DEVELOPMENT_MODE: | |
| # Do something for development | |
| print("I will do something specific for development!") | |
| else: | |
| print("I'm not in development mode so I will do nothing :(") |
Author
So.... How to user it? Any tips?
@tauantorres So this Gist is for my ancient article of 3 years ago in Japanese: https://zenn.dev/mkpoli/articles/a86fa78798fbaf
basically just put __environ__.py at the root dir, and import it to get ENV as the following:
from __environ__ import DEPLOYMENT_ENVIRONMENT
print("Current Deployment Environment is:", DEPLOYMENT_ENVIRONMENT)I uses VSCode's debug functionality (put launch.json to .vscode/launch.json) to give the environment variable.
When I have written this article and the gist, I was even using ancient pipenv. Nowadays, rye and other tooling are developed. After a quick search, I still couldn't find a good solution to give NODE_ENV-ish python equivalent, so maybe my solution could still be viable.
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
So.... How to user it? Any tips?