Logo

dev-resources.site

for different kinds of informations.

Dotenv: Python app environment variable vs. Linux environment variable

Published at
9/21/2023
Categories
python
linux
dotenv
environment
Author
ilija
Categories
4 categories in total
python
open
linux
open
dotenv
open
environment
open
Author
5 person written this
ilija
open
Dotenv: Python app environment variable vs. Linux environment variable

When it comes to usage of dotenv inside our python scripts there is one important caveat: if you use Linux you can not use env names like HOME or NAME or LOGNAME. Because your app variables from .env file (in currently working directory) will be overwritten by the global one (from Linux).

For example: in case you put in .env NAME=Michale and you are logged in as user Daniel, inside your python script os.getenv("NAME") will return Daniel instead of Michael. And most probably this is not something you want in your app....

I guess there is different ways to resolve this but most obvious for me is to avoid this names entirely and to use some convention like APP_NAME or APP_HOME_DIR....

Example:

local .env used in our Python script

NAME=Michale
USER=Michale
Enter fullscreen mode Exit fullscreen mode

Linux NAME and USER var for currently logged user (you can check with for example os.environ.keys())

NAME=Daniel
USER=Daniel

Enter fullscreen mode Exit fullscreen mode

test_env.py

import os
from dotenv import load_dotenv

load_dotenv()

name = os.getenv("NAME")
user = os.getenv("USER")

print(name, user)

Enter fullscreen mode Exit fullscreen mode

$ Daniel, Daniel

At this point inside your script you will have something you don't want and potentially break your program...

environment Article's
30 articles in total
Favicon
Understanding and Fixing the Externally-Managed-Environment Error
Favicon
Optimizing Environmental Resource Management with IoT and AI Integration
Favicon
The 3 Rules that 10x my Productivity (1 min read)
Favicon
Env-Core: An Easy Way to Validate Environment Variables in Node.js Projects
Favicon
Handling Environment Variables in Vite
Favicon
PDF Scan File Size: What To Do About It.
Favicon
Anaconda Installation and Virtual Environments: A Comprehensive Guide for Windows, Linux, and macOS
Favicon
A greener planet with tech
Favicon
Setting Up a Development Environment Using Laravel Sail (Docker)
Favicon
Daniel Siegel Alonso Suggests Sustainable Practices for the Music Industry
Favicon
@Environment variables
Favicon
Ongoing Carbon Emission Reduction Efforts Beneficial to Global Blue Ammonia Market Growth
Favicon
Parsing structured environment variables in Rust
Favicon
Sustainable Practices in Sports Turf Manufacturing: Reducing Environmental Footprint
Favicon
Adding Environment Variables to Your Webpack Project
Favicon
Building Environmental Analyzer using Lyzr SDK
Favicon
Playing with Python in node
Favicon
PHP on Manjaro with ASDF
Favicon
Harnessing Solar Power: The Key to Environmental Protection
Favicon
Navigating Challenges in Scaling Your Engineering Team: Strategies for Success
Favicon
Displaying air quality data in the terminal
Favicon
Dotenv: Python app environment variable vs. Linux environment variable
Favicon
Uncaught ReferenceError: process is not defined
Favicon
How to Get Environment Specific Configuration at Runtime in Angular
Favicon
How cryptocurrency destroys environment
Favicon
Python Virtual Environments
Favicon
shell 變數與環境變數
Favicon
The intersectionality of web performance
Favicon
Setting up an Alias for a Directory in Apache2 Server
Favicon
PyEnv & Poetry - BFFs 💖

Featured ones: