I need help on how to call specific variables depending on the environment. Currently, the project's root contains .env file that contains all environment variables being used by the project, but I need to load it according to environment
local -> where development happens
staging -> where we test our project
production -> where we deploy our final project
So in my project root, the goal is to use the file that contains specific values for variables depending on the environment where the [project runs. We have 3 files for the environment variables
local.env
INTEGRATION_URL=http://sandbox.api.com
staging.env
INTEGRATION_URL=http://sandbox2.api.com
production.env
INTEGRATION_URL=http://production.api.com
So inside client.py
from dotenv import load_dotenv
load_dotenv(os.getenv('local.env'))
class Client(BaseClient):
def _init_(self):
self.endpoint = os.environ.get('INTEGRATION_URL') // it should be `http://sandbox.api.com` if I run the project locally
But with this implementation, it still loads the default environment variables defined in the .env file. I need to remove the.env file and just used the 3 env files
Any help?
I solved it by removing all variables in .env, and putting a new variable that would specify which file should be called
.env
ENV_PATH=local.env // or prod.env // or staging.env
Then in client.py
from dotenv import load_dotenv
load_dotenv(os.getenv('ENV_PATH'), 'prod.env')// default to prod if not set
class Client(BaseClient):
def _init_(self):
self.endpoint = os.environ.get('INTEGRATION_URL')
Related
I'm working on node project, I'm using .env file to hide some of data. The .env file worked normally, and after I added more informations it got some color letters for variable names and it won't detect my variables when i'm using:
mongodb+srv://MONGODB_USERNAME:MONGODB_PASSWORD#cluster0.ekxmb.mongodb.net/MONGODB_DATABASE_NAME?retryWrites=true&w=majority
I'm having following code in my .env file:
MONGODB_PASSWORD = testpassword;
MONGODB_USERNAME = testuser;
MONGODB_DATABASE_NAME = testdbname;
It works when I manually type in app.js file code like this:
"mongodb+srv://testuser:testpassword#cluster0.ekxmb.mongodb.net/testdbname?retryWrites=true&w=majority"
After I added some additional variables code got formed weirdly with colors on variable names.
Note: It worked with code where I'm importing variables from .env file before, but after weird .env format it won't work.
Putting a variable name in a string will just make it say that variable name. In addition, you don't have the environment variables loaded anywhere.
Install the dotenv npm package for processing the .env file. Then, add this to your code to load the config file:
require('dotenv').config();
Create variables for each of your environment variables from process.env.YOUR_VARIABLE_NAME. An easy way to do this is with destructuring:
let {MONGODB_USERNAME, MONGODB_PASSWORD, MONGODB_DATABASE_NAME} = process.env;
Properly insert these variables into the string. You can use template literals for this:
`mongodb+srv://${MONGODB_USERNAME}:${MONGODB_PASSWORD}#cluster0.ekxmb.mongodb.net/${MONGODB_DATABASE_NAME}?retryWrites=true&w=majority`
I'm a total newbie and I'm trying to do this project this is my first time, and it's almost done. I tried every method mentioned in this SO thread to move secret key from settings. In every method i got some kind of error, even from this official django doc mathod. I couldn't find where I'm making mistake.
When the secret key is inside the settings.py, everything is working super smooth. But I need to push my code in git, so i have to hide it from settings.py.
Right now im adding the details when i tried using django-environ, to keep secret key outside of settings.py.
im putting the contents inside the root project folder.
im using miniconda: 4.10.1. here is my requirement.txt.
# platform: linux-64
_libgcc_mutex=0.1=main
_openmp_mutex=4.5=1_gnu
appdirs=1.4.4=py_0
asgiref=3.3.4=pyhd3eb1b0_0
attrs=21.2.0=pyhd3eb1b0_0
black=19.10b0=py_0
ca-certificates=2021.5.30=ha878542_0
certifi=2021.5.30=py39hf3d152e_0
click=8.0.1=pyhd3eb1b0_0
django=3.2.4=pyhd3eb1b0_0
django-environ=0.4.5=py_1
importlib-metadata=3.10.0=py39h06a4308_0
krb5=1.17.1=h173b8e3_0
ld_impl_linux-64=2.35.1=h7274673_9
libedit=3.1.20210216=h27cfd23_1
libffi=3.3=he6710b0_2
libgcc-ng=9.3.0=h5101ec6_17
libgomp=9.3.0=h5101ec6_17
libpq=12.2=h20c2e04_0
libstdcxx-ng=9.3.0=hd4cf53a_17
mypy_extensions=0.4.1=py39h06a4308_0
ncurses=6.2=he6710b0_1
openssl=1.1.1k=h7f98852_0
pathspec=0.7.0=py_0
pip=21.1.2=py39h06a4308_0
psycopg2=2.8.6=py39h3c74f83_1
python=3.9.5=h12debd9_4
python_abi=3.9=1_cp39
pytz=2021.1=pyhd3eb1b0_0
readline=8.1=h27cfd23_0
regex=2021.4.4=py39h27cfd23_0
setuptools=52.0.0=py39h06a4308_0
six=1.16.0=pyh6c4a22f_0
sqlite=3.35.4=hdfb4753_0
sqlparse=0.4.1=py_0
tk=8.6.10=hbc83047_0
toml=0.10.2=pyhd3eb1b0_0
typed-ast=1.4.2=py39h27cfd23_1
typing_extensions=3.7.4.3=pyha847dfd_0
tzdata=2020f=h52ac0ba_0
wheel=0.36.2=pyhd3eb1b0_0
xz=5.2.5=h7b6447c_0
zipp=3.4.1=pyhd3eb1b0_0
zlib=1.2.11=h7b6447c_3
settings.py
import os
import environ
from pathlib import Path
env = environ.Env(
# set casting, default value
DEBUG=(bool, False)
)
# reading .env file
environ.Env.read_env()
# Build paths inside the project like this: BASE_DIR / 'subdir'.
BASE_DIR = Path(__file__).resolve().parent.parent
# Quick-start development settings - unsuitable for production
# See https://docs.djangoproject.com/en/3.2/howto/deployment/checklist/
# SECURITY WARNING: keep the secret key used in production secret!
SECRET_KEY = env('SECRET_KEY')
# False if not in os.environ
DEBUG = env('DEBUG')
im not adding the rest of settings. i dont think its important. if need please mention I.ll update.
i placed .env file in root of the project where manage.py and db.sqlite3 are placed
.env
#env file
DEBUG=on
#copied the entire line from settings.py
SECRET_KEY ='xxxx django secret key here xxxx'
while running "python manage.py runserver", i got this error.
im not sure what im missing. i got some kind of error, when i tried each method and errors are not same. sorry that i cannot explain every method and error here.
there are several questions asked in this form. but most are not answered and some are not accurately explains my situation. please mention if anything else is needed or for more clarification.
First check that you have installed django-environ and maybe you have a typing mistake in your requirements.txt it should be django-environ=0.4.5 instead of django-environ=0.4.5=py_1
you can pass the path of your .env inside read_env(env_file="relative_path_of_your_env_file")
it read a .env file into os.environ.
If not given a path to a dotenv path, does filthy magic stack backtracking
to find manage.py and then find the dotenv.
go through this code https://github.com/joke2k/django-environ/blob/master/environ/environ.py#L614
From the structure of the file tree, its clear that .env file is placed in the root folder of the project. When checking the error message, its visible that whoever is searching for .env file is checking at the same place as settings.py.
So, the short answer is if you are using django-environ to keep secret-key outside, place .env file together with settings.py in the same directory.
For a bit more elaborated content, you can refer to this link. I felt it is suitable for newbies.
I am using python-decouple 3.4 for setting up environment variables for my django application. My .env file is in the same directory as that of manage.py. Except for SECRET_KEY (in settings.py), loading other environment variables in either settings.py or views.py directly fails stating that they have not been defined. The other environment variables which give error will be used in views.py.
Here is my .env file:-
SECRET_KEY=<django_app_secret_key>
file_path=<path_to_the file>
If I try to define them in settings.py like:-
from decouple import config
FILE_PATH = config('file_path')
and then use them in views.py,
from django.conf.settings import FILE_PATH
print(FILE_PATH)
then also I get the same error. How can I define environment variable for my views.py specifically?
[Edit: This is the error which I get:-
raise UndefinedValueError('{} not found. Declare it as envvar or define a default value.'.format(option))
decouple.UndefinedValueError: file_path not found. Declare it as envvar or define a default value.
whether I used this
from decouple import config
FILE_PATH = config('file_path')
in settings.py directly or views.py directly or first in settings.py and then in views.py like the example shown above]
I reproduced the same code you explained and it is working for me. The .env file should be in the root folder where manage.py exists. Make sure you are referencing the same settings file:
python manage.py runserver --settings=yourproj.settings.production
In the .env file:
file_path='/my/path'
In the settings.py file:
from decouple import config
FILE_PATH = config('file_path')
Also in the views.py file import the should be like this:
from django.conf import settings
print(settings.FILE_PATH)
In the .env file, the values assigned to variables were not enclosed in qoutes and that was why it was giving the error that it was unable to find file_path variable.
The .env file should be like this:-
SECRET_KEY='<django_app_secret_key>'
file_path='<path_to_the file>'
Anyways thanks to #Iain Shelvington and #Prakash S for your help.
For a project I need to define the ENVIRO variable (and some others) with the values prod/stage/dev.
This variable is used in .devcontainer/docker-compose.yml, .devcontainer/Dockerfile, some shell scripts and the Python source to set paths and the like.
Therefore I defined the file ${workspaceFolder}/.env which is imported by the Python extension like described here:
ENVIRO=dev
...
To avoid to execute / debug my code in the wrong environment, I wanted to create a little VSC extension, which does nothing else than to show the value of the ENVIRO variable in the Status Bar at the bottom.
Now the problem. In the extensions activate function I don't get access to the ENV variables defined in .env file:
const envValue = process.env["ENVIRO"];
// gives: undefined
In an terminal in the same VSC instance:
echo $ENVIRO
# gives: dev
When I access ENV variables defined by the system (not the .env file), there is no problem to access them in the extension's activate function:
export function activate( context: vscode.ExtensionContext) {
const envValue = process.env["NVM_BIN"];
// gives: '/Users/andi/.nvm/versions/node/v14.15.1/bin'
Is there no way to access this variable?
My suspicion is following:
The Python extension extends the Environment with the variables using the EnvironmentVariableCollection
This adds them to the terminal environment, but prevents access to the variables in other extensions.
Or do I (hopefully) miss something?
I have the following configuration file in /etc/sysconfig/myconf:
export USER=root
export NODE_DIR=/opt/MyDir
I want to use these setting in my .js file, which located in /opt/myapplication:
var userApp = //USER in /etc/sysconfig/myconf file
var dir = //NODE_DIR in /etc/sysconfig/myconf file
Is there any way to do it without open the file and parse it contents?
As I understand the export should give me the option to read it easily in node.js, but I don't find how (In addition, when I run export -p, I don't see these variables)
EDIT: what I search is equal Node.js's command to source command in Linux (the variables is not environment variables)
If those environment variables are available when you launch the program, you can use process.env. https://nodejs.org/api/process.html#process_process_env