-
Notifications
You must be signed in to change notification settings - Fork 430
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: load multiple .env
file before running a command
#418
Comments
I'm also interested in this. I see two use cases where this is useful:
I gave it a try and have a working proof of concept in #424 . It's not complete, but I can work on it if there's interest. |
@duarte-pompeu Thanks for your work ! Definitely interested by this feature I can contribute if needed, let me know |
FYI I was able to find a workaround. Given the following files: # test1.env
URL="https://www.example.com"
USER="user"
PASSWORD="secret"
# test2.env
USER="admin"
PASSWORD="admin"
# test.py
import os
print(os.environ["URL"])
print(os.environ["USER"])
print(os.environ["PASSWORD"]) We can combine them by chaining multiple calls to dotenv: $ dotenv -f src/test1.env run -- dotenv -f src/test2.env run -- python src/test.py
https://www.example.com
admin
admin |
Great! I was having some trouble with multiple warnings from What do you think about this @theskumar ? |
I'm not sure if my use case is remotely similar to what is being discussed here, but I have something like a list of users and password combination. The actual data is much more complex than that, but the current way I can achieve what I want is defining dictionaries in many package files then importing them:
expected output:
Moral of the story: I want to be able to read variables from several files, and I want to be able to select which ones at each run. But I'm not supposed to overwrite those variables with the last read file. I want instead to append every file to a "list of envs". Perhaps this is out of scope but that is my requirement. |
@iuriguilherme: I think that's a different scenario: in this issue we want to override values, not use multiple ones. Anyawy, I'll try to help you. Would this work? Files: # user1.env
username="user_1"
password="pass_1"
# user2.env
username="user_2"
password="pass_2" Python REPL: >>> from dotenv import *
>>> users_dicts = [dotenv_values(env) for env in ["user1.env", "user2.env"]]
>>> users_dicts
[OrderedDict([('username', 'user_1'), ('password', 'pass_1')]), OrderedDict([('username', 'user_2'), ('password', 'pass_2')])]
>>> from dataclasses import *
>>> @dataclass
... class User:
... username: str
... password: str
...
>>> users
[User(username='user_1', password='pass_1'), User(username='user_2', password='pass_2')]
>>>
>>> users = [User(**user_dict) for user_dict in users_dicts]
>>> users[0].username
'user_1'
>>> users[1].username
'user_2' You may also want to take a look at pydantic, which is more advanced than python's dataclasses: https://pydantic-docs.helpmanual.io/ |
That's exactly what I'm trying to do, use pydantic BaseSettings in
conjunction with dotenv. Because there are scenarios where I can't import
configuration files as modules or packages, and it's more reliable to use
dotenv files, configparser, etc.
Example of a case that my software breaks if the user fail to provide
appropriate configuration files and there is no easy way to debug what's
wrong is `pip install -e .`
|
@duarte-pompeu will you still carry this forward or can/should someone else help/take over? |
I attempted to implement this on #424 but didn't get any feedback. If a maintainer is interested in merging this feature, I can fix the merge conflicts. |
I have different configuration for my application that I intend to be composable. For example imagine I have env files with config values for database server hosts, and other env files with user credentials. I want to choose a combination of the two. I try to do something like
dotenv -f host_config.env -f user_credentials.env run bash do_things_with_db.sh
However it seems that only the last file is taken into account. I think this would be a useful feature to be able to load multiple files. What do you people think about it?
The text was updated successfully, but these errors were encountered: