How to get python-decouple to work on a live server?

1.1k views Asked by At

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

There are 2 answers

0
zhanymkanov On

Yes, you should not store vulnerable data such as .env files in public repositories.

But, you should create it again on the server-side.

  1. Connect to the server
  2. Go to the project folder
  3. Create .env file and fulfill it with all necessary variables you use.
0
apollos On

From my findings I think there is just one way using every environment, either local and live server. First you need to install python-decouple library in conjunction with .env file as follows:

  1. Install Python-Decouple:

    pip install python-decouple

  2. Create a .env file in the root directorate of your project and follow this format to define your Environment Variables inside the file.

    SECRET_KEY = secret_key_for_app_you_may_use_code_to_it_unique

    DATABASE_NAME = name_of_your_database

    DATABASE_USER = your_database_username

    DATABASE_PASSWORD = your_database_password

Notice there are no quotation marks in the values for the variables. 3. Import the config method in settings.py and use it to call these variables as follows:

from decouple import config

Now Call the variables in the appropriate places using the config method

DATABASES = {
'default': {
    'ENGINE': 'django.db.backends.postgresql',
    'NAME': config('DATABASE_NAME'),
    'USER': config('DATABASE_USER'),
    'PASSWORD': config('DATABASE_PASS'),
    'HOST': 'localhost'
}
}

With this method, if you are hosting on server that is using CLI or Cpanel they will work fine in either ways, either by uploading the .env file to your web server (Cpanel) and doing just as I have done here or adding Environment Variable under the Python Application with Key Value pairs, install Python-Decouple, import config from decouple in settings.py and then call them as shown above. I think this should help you. thanks