Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is there a NODE_ENV equivalent in Python

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?

like image 423
Pubudu Dodangoda Avatar asked Nov 20 '17 13:11

Pubudu Dodangoda


People also ask

Can you override NODE_ENV?

You cannot override NODE_ENV manually. This prevents developers from accidentally deploying a slow development build to production.

Does jest set NODE_ENV?

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.

What does NODE_ENV mean?

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).

What does NODE_ENV default to?

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.


Video Answer


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).

like image 97
Ankit Bahuguna Avatar answered Oct 17 '22 13:10

Ankit Bahuguna


you can try environs library in python pip install environs

like image 40
Argus Malware Avatar answered Oct 17 '22 11:10

Argus Malware