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?
You cannot override NODE_ENV manually. This prevents developers from accidentally deploying a slow development build to production.
Jest automatically defines environment variable NODE_ENV as test (see https://jestjs.io/docs/environment-variables), as you can confirm from your error message: console.
NODE_ENV is an environment variable that stands for node environment in express server. The NODE_ENV environment variable specifies the environment in which an application is running (usually, development or production).
We see that it in fact reads NODE_ENV and defaults to 'development' if it isn't set. This variable is exposed to applications via 'app.
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With