Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add option to overwrite existing environment variables in read_env #191

Closed
wants to merge 2 commits into from

Conversation

kutenai
Copy link

@kutenai kutenai commented Jul 8, 2018

This PR addresses issue #103.
I've had this issue with the uwsgi touch-reload command. It won't pick up updates to .env files.

In some use cases, the .env file should be the master, so an overwrite=True option will resolve the issue in this case.

  • All tests pass
  • Should be no performance penalty for current use (other than an additional function call)
  • Minor performance in overwrite use due to temp dict.

The temp dict was needed since there is no dict.set(k,v) method. The lambda can't do dict[k] = v.

@coveralls
Copy link

coveralls commented Jul 8, 2018

Coverage Status

Coverage increased (+3.7%) to 89.376% when pulling 56772cb on sharpertool:develop into c262002 on joke2k:develop.

@coveralls
Copy link

coveralls commented Jul 8, 2018

Coverage Status

Coverage increased (+3.7%) to 89.401% when pulling e979562 on sharpertool:develop into c262002 on joke2k:develop.

@BernardoSilva
Copy link

This would be very useful to have 👍

tkdchen pushed a commit to tkdchen/django-environ that referenced this pull request Oct 6, 2019
_setup: do not call django.setup() if settings are configured already
@JakeUrban
Copy link

@joke2k Can we get this merged?

@joke2k joke2k added the enhancement New feature or request label Mar 15, 2020
@TmLev
Copy link

TmLev commented Sep 2, 2020

Any chance this will be merged soon?

@pymen
Copy link

pymen commented Nov 14, 2020

I think the idea that it should not override original env variables [os.environ]

You can use the following helper to merge envs from files


def merge_files_env_vars(*files):
    """Merge env variables from files.
    order of files is important, bc next file can override overlapping values from previous files
    
    files are processed in reverse order bc environ uses setdefault, 
    so the latest file env can not be overwritten by previous
    """
    env = environ.Env()
    env.ENVIRON = {}
    for file_path in reversed(files):
        env.read_env(file_path)
    
    return env.ENVIRON

and later load them by

    env = environ.Env()
    env_variables = merge_files_env_vars(*env_files)
    env.read_env(**env_variables)

@sergeyklay
Copy link
Collaborator

PR was closed by mistake. Reopened it.

@sergeyklay
Copy link
Collaborator

This is implemented in develop branch. Thank you for the pathc, and for helping us make django-environ better. And I am sorry about the delay.

@sergeyklay sergeyklay closed this Sep 27, 2021
@sergeyklay sergeyklay self-assigned this Sep 27, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
None yet
Development

Successfully merging this pull request may close these issues.

8 participants