question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

can not override environment settings that already have value with a file

See original GitHub issue

I have

env = environ.Env(
    DEBUG=(bool, False),
)
ENV_FILE = str(env.path('ENV_FILE', default='/'))
if os.path.isfile(ENV_FILE):
    overrides = {'DATABASE_URL': os.environ.pop('DATABASE_URL', '')}
    env.read_env(env_file=ENV_FILE, **overrides)
else:
    # unset if no file was found
    ENV_FILE = None

Problem is that in my ENV_FILE, I want to set DATABASE_URL that already has a value when reading the ENV_FILE.

But because def read_env(env_file=None, **overrides) uses setdefault(key, str(val)) this value can not be overwritten.

Is it possible to extend def read_env with an extra boolean argument to decide whether or not to override existing environment variables?

Issue Analytics

  • State:closed
  • Created 7 years ago
  • Reactions:1
  • Comments:13 (2 by maintainers)

github_iconTop GitHub Comments

5reactions
patrickjrmcommented, Feb 22, 2019

+1 for merging PR #191.

The similar python-dotenv project does have an override=True option.

https://github.com/theskumar/python-dotenv#getting-started

4reactions
mpochroncommented, Mar 4, 2021

Simple to load env file to override environments from settings:

overrides = {key: os.environ.pop(key, value) for key, value in os.environ.items()}
environ.Env.read_env(env_file=str(ROOT_DIR.path('.env')), **overrides)
Read more comments on GitHub >

github_iconTop Results From Across the Web

c# - can't override appsettings.json settings with environment ...
Update 2: I restarted my computer and now I see my Environment variable in the list, but it doesn't override the value in...
Read more >
Overriding One Single Value in Your Docker-Compose .env File
env file is reasonably simple: just set an environment variable with the same name in your shell before running your docker-compose command. This...
Read more >
Environment variables in Compose | Docker Documentation
You can override the environment file path using a command line argument --env-file . Using the “--env-file” option . By passing the...
Read more >
Why isn't my ASP.NET Core environment-specific ...
There are a number of ways you could achieve this, for example: Overwrite default values e.g. only set an environment variable for the...
Read more >
Use multiple environments in ASP.NET Core | Microsoft Learn
json override values set in the system environment. The launchSettings.json file: Is only used on the local development machine. Is not deployed ...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found