HBNetwork / python-decouple

Strict separation of config from code.
MIT License
2.79k stars 192 forks source link

How to give path of .env files? #106

Closed talhaanwarch closed 3 years ago

talhaanwarch commented 3 years ago

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.

henriquebastos commented 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 .

talhaanwarch commented 3 years ago

how can i inform the path? i mean how can I pass the argument, or set the path

madjaqk commented 3 years ago

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.

sidmitra commented 3 years ago

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.

henriquebastos commented 3 years ago

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.

henriquebastos commented 3 years ago

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.

henriquebastos commented 3 years ago

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.

henriquebastos commented 3 years ago

Replied every one. Closing now.

halla commented 2 years ago

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.

henriquebastos commented 2 years ago

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.

halla commented 2 years ago

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).

alercelik commented 11 months ago

Modifying the code a little bit more

from decouple import Config, RepositoryEnv, config

config = Config(RepositoryEnv(config("DOTENV_FILE", ".env")))