Is there a NODE_ENV equivalent in Python

Viewed 5141

Is there a NODE_ENV equivalent in Python?

I want to dynamically load JSON configurations to the python application based on the executing environment. In nodeJs, I do this by using the process.env.NODE_ENV.

For example,

I start the app like this,

NODE_ENV=production node server.js

And use the variable in the application like this,

if(process.env.NODE_ENV == "production") {
    // Load the production config file here (eg: database.json)
} else {
    // Load the development config file here (located in a different directory)
}

How can I achieve the same in Python? Or can I make use of python virtualenv or python setuptools to have a workaround?

2 Answers

For starters, you could do something like this.

import os
env = os.environ.get("PYTHON_ENV")
if(env =="production"):
   //
else :

The script can be run with PYTHON_ENV="production" python myscript.py. Or you could use some library like dotenv(https://github.com/theskumar/python-dotenv).

you can try environs library in python pip install environs

Related