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

Improve Dotenv usage to avoid basic issues #350

Open
sveetch opened this issue Feb 5, 2023 · 2 comments
Open

Improve Dotenv usage to avoid basic issues #350

sveetch opened this issue Feb 5, 2023 · 2 comments

Comments

@sveetch
Copy link

sveetch commented Feb 5, 2023

Feature Request

Is your feature request related to a problem or unsupported use case?

I tried to implement the Dotenv feature in my settings and i encountered some issues when setting it from a setup method. I did this because i have settings file for environments and i though it would be nice to have a dotenv file for each environment (like .env.dev, .env.production, etc..)

Setting from 'post_setup'

Trying to set the DOTENV in post_setup don't work, which i understood lately seeing the Dotenv mechanism (Configuration.load_dotenv) is done from Configuration.pre_setup. Result is just the Dotenv path in post_setup is just ignored (i didn't tried in setup() but i think it will be the same);

Mistake sample:

class Development(Configuration):
    @classmethod
    def post_setup(cls):
        super().post_setup()

        cls.DOTENV = "/foo/bar/.env"

Setting from 'pre_setup'

Trying to set the DOTENV in pre_setup can't work, since it would need to be before a super().pre_setup() to be effective. But trying to set it before is just impossible it leads to this error:

django.core.exceptions.AppRegistryNotReady: Apps aren't loaded yet.

It was hard to debug because i did not considered a problem with Dotenv setting and the setup methods. I just found it in going back to a previous version where i didn't set DOTENV in setup methods. The error traceback was not helpful since it did not involved any point to django-configurations code or my settings. Seems the usage of cls before the setup cause problems.

Mistake sample:

class Development(Configuration):
    @classmethod
    def pre_setup(cls):
        cls.DOTENV = "/foo/bar/.env"

        super().pre_setup()

Describe the solution you'd like

Update the documentation part which introduces DOTENV setting and put a warning to list these usage mistakes.

Describe alternatives you've considered

From what i saw i don't think the setup mechanism and settings loader are able to properly detect these usage mistakes.

Do you want to work on it through a Pull Request?

I could try to apply a documentation change in a pull request if needed. I didn't do it yet since i would want your advice on this issue before, since i'm not sure i missed something.

@sveetch
Copy link
Author

sveetch commented Feb 6, 2023

I'm sorry but after more digging, i've finally found that the issue with pre_setup() was wrong, in fact it was only that the Dotenv filepath did not exists. As i assumed that the Dotenv file loading was safe, i did not created a Dotenv filepath yet before testing.

I don't know why this leaded to the exception django.core.exceptions.AppRegistryNotReady, maybe because i'm composing my setting class from other classes, however i made the DOTENV conditionnal to the Dotenv filepath existence and it correctly works.

Just maybe insist in documentation about the defined Dotenv filepath in DOTENV must exists ?

@paduszyk
Copy link
Contributor

@sveetch I wanted to raise the same issue.

I resolved this by a simple check in pre_setup:

@classmethod
def pre_setup(cls):
    DOTENV = cls.BASE_DIR / ".env"

    if DOTENV.exists():
        cls.DOTENV = DOTENV

    super().pre_setup()

Now, the configuration pre-setup fails silently if the .env file is not found

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

No branches or pull requests

2 participants