Created
          February 5, 2022 18:13 
        
      - 
      
- 
        Save anandtripathi5/1ae8340718b77468dd0f987740e1ab6d to your computer and use it in GitHub Desktop. 
    Pydantic usage with python-dotenv
  
        
  
    
      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 typing import Union | |
| from pydantic import BaseSettings, Field | |
| class Base(BaseSettings): | |
| secret_key: str = Field('random_string', env='ANOTHER_SECRET_KEY') | |
| port: int = 5050 | |
| username: str = "ANAND" | |
| class Config: | |
| case_sensitive = False | |
| env_file = '.env' | |
| class Dev(Base): | |
| username = "TRIPATHI" | |
| class Config: | |
| env_file = 'dev.env' | |
| class Prod(Base): | |
| username = "Production" | |
| port = 5051 | |
| class Config: | |
| env_file = 'prod.env' | |
| config = dict( | |
| dev=Dev, | |
| prod=Prod | |
| ) | |
| settings: Union[Dev, Prod] = config[os.environ.get('ENV', 'dev').lower()]() | 
  
    Sign up for free
    to join this conversation on GitHub.
    Already have an account?
    Sign in to comment