DEV Community

Discussion on: Using .env Files for Environment Variables in Python Applications

Collapse
 
navporky profile image
NK • Edited

Hi Jake,

I am a noob, trying to learn python along with good practices. Now the queries: The official python-dotenv documentation talks about using the dotenv alongside settings module (python-settings). The load_dotenv() function is also recommended to be put in settings.py. Unable to wrap my head around the following:

  1. Though you have not advised it, why is it advised by many to use both? wouldn't load_dotenv work in any .py file?
  2. While going to pipenv shell for the project, the .env file is automatically loaded. What loads the settings.py file (which has code to load dotenv)?
  3. if .env is excluded from git via .gitignore then, is the other team developer/ tester support to make his own while looking at settings.py for what all values could have been there? and only after the error of not finding the .env file is encountered/ reading the readme.md file?
  4. In production, the environment variables are recommended to set at OS level? Why not in the same .env file, which can be manually created?
  5. For any environment - dev/ test/ production, the code could be written to produce a template .env file with just keys (Key=) and user (dev/ tester, prod) team member can just fill the value in before using. Is this not workable and logical way to do this?
  6. Just looked at python-decouple (as an alternate to python-settings) , won't this is even better as it can handle datatypes as well?

Thanks.

Collapse
 
hirushaadi profile image
Hirusha Adikari
  1. i think load_dotenv() would be enough (it always works on linux, where you would normally deploy a web app)
  2. you will have to import it manually and use. for testing of that file by running it directly, you can put it under if name == "main"
  3. you can add it to the docs of the program and keep a .env.example file filled with dummy values or no values at all (with just the structure)
  4. its secure to keep it away from application's code and config files. doing this, you can ensure that the data is only accessible to the application and not to other users or processes on the same machine
  5. haven't tried it