Closed talhaanwarch closed 3 years ago
It searches recursively on parent directories. If you choose some other branch, you'll have to inform it to decouple.
On Sun, Dec 6, 2020 at 8:08 AM Talha Anwar notifications@github.com wrote:
I want to know if I put my env in another directory of server for security purpose. How do decouple know where it is located.
— You are receiving this because you are subscribed to this thread. Reply to this email directly, view it on GitHub https://github.com/henriquebastos/python-decouple/issues/106, or unsubscribe https://github.com/notifications/unsubscribe-auth/AAAC6IS74ZV6A6ESCUSQOZLSTNQ3HANCNFSM4UPHVO2A .
how can i inform the path? i mean how can I pass the argument, or set the path
I don't know if this is documented anywhere, but this is what I was able to work out from looking through the code:
from decouple import Config, RepositoryEnv
config = Config(RepositoryEnv("path/to/env_file"))
I would also like to know if there's a better/more official way I should be doing this.
Along similar lines, I have multiple env files eg. .env.local
or .env.ci
and in some cases i want to be able to run the same code/server with different env files. I couldn't find a way to get decouple to change the name of the file, atleast from reading the code.
I could probably modify the code from @madjaqk a bit to do it this way.
import os
from decouple import Config, RepositoryEnv
DOTENV_FILE = os.environ.get("DOTENV_FILE", ".env") # only place using os.environ
config = Config(RepositoryEnv(DOTENV_FILE))
That would allow me to do something like this.
DOTENV_FILE=".env" python server.py # or at a different terminal
DOTENV_FILE=".env.second" python another_server.py
I will try it out later today.
Not sure if this feature is useful enough for others to include. If so, i can send a PR to support a dynamic SUPPORTED
var.
how can i inform the path? i mean how can I pass the argument, or set the path
@talhaanwarch you should protect your code from having to deal with server layout particularities. What I do is, during build/deployment, simple add a symlink to a read only file as .env and all would workout fine.
If so, i can send a PR to support a dynamic
SUPPORTED
var.
Thank you, @sidmitra. But this would work against python-decouple’s design assumptions.
As explained before, the whole idea is that your code should be decouples from instance configuration and layout.
I don't know if this is documented anywhere, but this is what I was able to work out from looking through the code:
from decouple import Config, RepositoryEnv config = Config(RepositoryEnv("path/to/env_file"))
I would also like to know if there's a better/more official way I should be doing this.
That’s the right way to do it @madjaqk. It's not documented because it is not encouraged. Your code should not know about it’s config file. From your code perspective everything is up and running from proper environment variables.
Replied every one. Closing now.
I may be overlooking something but I fail to see the downside in @sidmitra 's solution. To me it seems to decouple things even more, in this case from the specific project layout with predefined name and location for the config file.
Hi @halla. The lib is already decoupled from the .env
file since you can specify an alternative. I rather not introduce an environment variable into a lib that processes environment variables.
Right, I would probably also avoid a separate feature now that I think of it, as there is an easy solution already. I guess I was reacting more to the decision not to document it (which was actually an answer to another similar solution but without the environment variable part, to be exact).
Modifying the code a little bit more
from decouple import Config, RepositoryEnv, config
config = Config(RepositoryEnv(config("DOTENV_FILE", ".env")))
I want to know if I put my env in another directory of server for security purpose. How do decouple know where it is located.