1

I am using python-decouple 3.4 for setting up environment variables for my django application. My .env file is in the same directory as that of manage.py. Except for SECRET_KEY (in settings.py), loading other environment variables in either settings.py or views.py directly fails stating that they have not been defined. The other environment variables which give error will be used in views.py.

Here is my .env file:-

SECRET_KEY=<django_app_secret_key>
file_path=<path_to_the file>

If I try to define them in settings.py like:-

from decouple import config
FILE_PATH = config('file_path')

and then use them in views.py,

from django.conf.settings import FILE_PATH
print(FILE_PATH)

then also I get the same error. How can I define environment variable for my views.py specifically?

[Edit: This is the error which I get:-

raise UndefinedValueError('{} not found. Declare it as envvar or define a default value.'.format(option))
decouple.UndefinedValueError: file_path not found. Declare it as envvar or define a default value.

whether I used this

from decouple import config
    FILE_PATH = config('file_path')

in settings.py directly or views.py directly or first in settings.py and then in views.py like the example shown above]

zester
  • 165
  • 3
  • 12
  • 1
    You shouldn't try importing a setting directly, import the `settings` object and access the setting as an attribute lookup : `from django.conf import settings; print(settings.FILE_PATH)` – Iain Shelvington Jun 18 '21 at 08:13
  • @IainShelvington Thanks for your suggestion but it turns out that the error is because of the config line (**FILE_PATH = config('file_path')**) in settings.py itself. I have added the error in the question. – zester Jun 18 '21 at 08:55
  • Show your .env file also. Is that file is in your root folder ? Also as Iain mentioned it should be 'from django.conf import settings' – Prakash S Jun 18 '21 at 09:06
  • I have added the .env file to the question. By root, do you mean root folder of django project or root of system? As I mentioned before, the error is in settings.py itself and not because of from django.conf import settings – zester Jun 18 '21 at 09:29

2 Answers2

3

I reproduced the same code you explained and it is working for me. The .env file should be in the root folder where manage.py exists. Make sure you are referencing the same settings file:

python manage.py runserver --settings=yourproj.settings.production

In the .env file:

file_path='/my/path'

In the settings.py file:

from decouple import config
FILE_PATH = config('file_path')

Also in the views.py file import the should be like this:

from django.conf import settings
print(settings.FILE_PATH)
Prakash S
  • 632
  • 6
  • 12
0

In the .env file, the values assigned to variables were not enclosed in qoutes and that was why it was giving the error that it was unable to find file_path variable.

The .env file should be like this:-

SECRET_KEY='<django_app_secret_key>'
file_path='<path_to_the file>'

Anyways thanks to @Iain Shelvington and @Prakash S for your help.

zester
  • 165
  • 3
  • 12