Forums

Django settings: Environment variables Vs Import Python code

Hello, I have just read and implemented your recommendations about using environment variables in settings.py (https://help.pythonanywhere.com/pages/environment-variables-for-web-apps) for information that should not be saved in the code repository.

Isn't there also the possibility of simply using Python variables initialised in another Python file (included in .gitignore file) and imported in settings.py. For example, that Python file would include :

SECRET_KEY_val="asdfghjk"
DATABASE_NAME="myuser$database"
DATABASE_HOST= ...

This way we would not need to add code in the wsgi.py configuration and install the python-dotenv library. What would be the inconvenients of that approach compared to using environment variables?

Sure, you can use that approach, too. Whatever works for you.

That is an excelent idea, I am wondering the same is there are anything missing as I prefer this method than installing and using .env