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

Feature Request: Add Support for Prefix in Env Constructor #384

Closed
arthurc0102 opened this issue Jan 9, 2025 · 6 comments · Fixed by #387
Closed

Feature Request: Add Support for Prefix in Env Constructor #384

arthurc0102 opened this issue Jan 9, 2025 · 6 comments · Fixed by #387

Comments

@arthurc0102
Copy link

Hi,

I have a use case where all required environment variables need to share a common prefix. To simplify configuration and avoid redundancy, I propose adding a prefix argument to the Env constructor. For example:

from environs import Env

env = Env(prefix='MYAPP_')

# Instead of accessing `env('MYAPP_VAR')`, we could directly access:
value = env('VAR')  # Automatically resolves to 'MYAPP_VAR'

This feature could enhance the usability of environs for projects that follow naming conventions for environment variables.

If this idea aligns with the project's direction, I'd be happy to implement it and submit a pull request.

@sloria
Copy link
Owner

sloria commented Jan 9, 2025

@sloria sloria closed this as completed Jan 9, 2025
@arthurc0102
Copy link
Author

I know environs already supported, but it requires using the context manager approach. I am using it in Django to read settings, and all my settings start with DJANGO_ prefix. Following the previous implementation, it was not very convenient. Therefore, I proposed allowing users to pass in a prefix at the beginning.

@sloria
Copy link
Owner

sloria commented Jan 10, 2025

@arthurc0102 why is the context manager less convenient?

@sloria sloria reopened this Jan 10, 2025
@arthurc0102
Copy link
Author

The current usage is as follows (part of settings.py):

env = environs.Env()
env.read_env(path=str(BASE_DIR / ".env"), recurse=False)

# Quick-start development settings - unsuitable for production
# See https://docs.djangoproject.com/en/5.1/howto/deployment/checklist/

# SECURITY WARNING: keep the secret key used in production secret!
SECRET_KEY = env.str("SECRET_KEY")

# SECURITY WARNING: don't run with debug turned on in production!
DEBUG = env.bool("DEBUG", default=(MODE == "local"))

ALLOWED_HOSTS = env.list("ALLOWED_HOSTS", default=[])

All environment variables here start with DJANGO_, e.g., DJANGO_SECRET_KEY. In this scenario, we would need to wrap the entire file in a context manager or write multiple with statements.

@sloria
Copy link
Owner

sloria commented Jan 10, 2025

yeah, I suppose if all your environment variables have the same prefix, the ctor argument is slightly more convenient and avoids having to indent the whole file.

i've implemented this in #387

@arthurc0102
Copy link
Author

Thank you for implementing this feature and releasing a new version!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants