I can't seem to get python-decouple to work on a live server. I have a Django project in which python-decouple is used to separate my important variables such as secret key, debug, database vars, and so on in a .env file. When the project is run locally, python-decouple works well and I have no problems.
After confirming that it works well locally, I then added the .env file to my .gitignore file, pushed the code to my GitHub repository, then pulled the code to the AWS live server.
However, when trying to make migrations while configuring the project on an AWS EC2 Ubuntu server, I get the error below. It seems as though the variables in the .env file are not being applied on the AWS Ubuntu server which would make sense because of the .gitignore file. But I thought the whole point of python-decouple was to not have the important variables on the live server. Does anyone know why this error is occurring? and how I can fix it? or do not fully understand the use cases of python-decouple?
$ python manage.py makemigrations
Traceback (most recent call last):
File "manage.py", line 15, in <module>
execute_from_command_line(sys.argv)
File "/home/theo/aggtrends/aggenv/lib/python3.6/site-packages/django/core/management/__init__.py", line 401, in execute_from_command_line
utility.execute()
File "/home/theo/aggtrends/aggenv/lib/python3.6/site-packages/django/core/management/__init__.py", line 345, in execute
settings.INSTALLED_APPS
File "/home/theo/aggtrends/aggenv/lib/python3.6/site-packages/django/conf/__init__.py", line 76, in __getattr__
self._setup(name)
File "/home/theo/aggtrends/aggenv/lib/python3.6/site-packages/django/conf/__init__.py", line 63, in _setup
self._wrapped = Settings(settings_module)
File "/home/theo/aggtrends/aggenv/lib/python3.6/site-packages/django/conf/__init__.py", line 142, in __init__
mod = importlib.import_module(self.SETTINGS_MODULE)
File "/usr/lib/python3.6/importlib/__init__.py", line 126, in import_module
return _bootstrap._gcd_import(name[level:], package, level)
File "<frozen importlib._bootstrap>", line 994, in _gcd_import
File "<frozen importlib._bootstrap>", line 971, in _find_and_load
File "<frozen importlib._bootstrap>", line 955, in _find_and_load_unlocked
File "<frozen importlib._bootstrap>", line 665, in _load_unlocked
File "<frozen importlib._bootstrap_external>", line 678,
in exec_module
File "<frozen importlib._bootstrap>", line 219, in _call_with_frames_removed
File "/home/theo/aggtrends/aggregatetrends/settings.py",
line 26, in <module>
SECRET_KEY = config('SECRET_KEY')
File "/home/theo/aggtrends/aggenv/lib/python3.6/site-packages/decouple.py", line 199, in __call__
return self.config(*args, **kwargs)
File "/home/theo/aggtrends/aggenv/lib/python3.6/site-packages/decouple.py", line 83, in __call__
return self.get(*args, **kwargs)
File "/home/theo/aggtrends/aggenv/lib/python3.6/site-packages/decouple.py", line 68, in get
raise UndefinedValueError('{} not found. Declare it as
envvar or define a default value.'.format(option))
decouple.UndefinedValueError: SECRET_KEY not found. Declare it as envvar or define a default value.
Yes, you should not store vulnerable data such as
.envfiles in public repositories.But, you should create it again on the server-side.
.envfile and fulfill it with all necessary variables you use.